How can I both stroke and fill with NSAttributedString w/ UILabel

Yes, the key is to apply a Negative value to the NSStrokeWidthAttributeName If this value is positive you will only see the stroke and not the fill. Objective-C: self.label.attributedText=[[NSAttributedString alloc] initWithString:@”string to both stroke and fill” attributes:@{ NSStrokeWidthAttributeName: @-3.0, NSStrokeColorAttributeName:[UIColor yellowColor], NSForegroundColorAttributeName:[UIColor redColor] } ]; Thanks to @cacau below: See also Technical Q&A QA1531 Swift … Read more

Add left/right horizontal padding to UILabel

For a full list of available solutions, see this answer: UILabel text margin Try subclassing UILabel, like @Tommy Herbert suggests in the answer to [this question][1]. Copied and pasted for your convenience: I solved this by subclassing UILabel and overriding drawTextInRect: like this: – (void)drawTextInRect:(CGRect)rect { UIEdgeInsets insets = {0, 5, 0, 5}; [super drawTextInRect:UIEdgeInsetsInsetRect(rect, … Read more

How to set kerning in iPhone UILabel

Old question, but you can do it now (easily). NSMutableAttributedString *attributedString; attributedString = [[NSMutableAttributedString alloc] initWithString:@”Please get wider”]; [attributedString addAttribute:NSKernAttributeName value:@5 range:NSMakeRange(10, 5)]; [self.label setAttributedText:attributedString]; For Nov 2013, Just to expand on this great answer, here’s some totally typical code. Usually you’d set the font as well. Note in the comments the old-fashioned way using … Read more

Two colors for UILabel TEXT

you can set text color with pattern image like bellow.. [yourLable setTextColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@”yourImageName”]]]; and also set different color with this bellow code.. please check tutorial with detail mate.. NSString *test = @”Hello. That is a test attributed string.”; CFStringRef string = (CFStringRef) test; CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0); CFAttributedStringReplaceString (attrString,CFRangeMake(0, 0), string); /* Note: … Read more

How do I calculate the UILabel height dynamically [duplicate]

The easiest way to get the height is sizeThatFits. Use it like this: Objective-C CGFloat maxLabelWidth = 100; CGSize neededSize = [label sizeThatFits:CGSizeMake(maxLabelWidth, CGFLOAT_MAX)]; Swift 3.0 let maxLabelWidth: CGFloat = 100 let neededSize = label.sizeThatFits(CGSize(width: maxLabelWidth, height: CGFloat.greatestFiniteMagnitude)) The height your label needs is neededSize.height. Note that im using CGFLOAT_MAX for the size height, to … Read more

Letter by letter animation for UILabel?

Update for 2018, Swift 4.1: extension UILabel { func animate(newText: String, characterDelay: TimeInterval) { DispatchQueue.main.async { self.text = “” for (index, character) in newText.enumerated() { DispatchQueue.main.asyncAfter(deadline: .now() + characterDelay * Double(index)) { self.text?.append(character) } } } } } calling it is simple and thread safe: myLabel.animate(newText: myLabel.text ?? “May the source be with you”, characterDelay: … Read more

How to change an UILabel/UIFont’s letter spacing?

From iOS 6 you can use NSAttributedString in UILabel. In attributed string you can use attribute NSKernAttributeName to set letter spacing NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString: @”Test test test test “]; [attrStr addAttribute:NSKernAttributeName value:@(4.0) range:NSMakeRange(0, attrStr.length)]; UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 300, 300, 100)]; label.attributedText = attrStr;