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

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 to add CSS for HTML to NSAttributedString?

Since iOS 7 you can use NSAttributedString with HTML syntax: NSURL *htmlString = [[NSBundle mainBundle] URLForResource: @”string” withExtension:@”html”]; NSAttributedString *stringWithHTMLAttributes = [[NSAttributedString alloc] initWithFileURL:htmlString options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil]; textView.attributedText = stringWithHTMLAttributes; // attributedText field! You have to add string.html to you project, and the content of the html can be like this: <html> <head> <style type=”text/css”> … Read more

iOS 7 BUG – NSAttributedString does not appear

I have found a workaround for this bug. In your github code, to use the workaround, you would say this: NSAttributedString* attrStr = [[NSAttributedString alloc] initWithString:@”Lorem ipsum dolor sit\n” // <— attributes: @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle), NSParagraphStyleAttributeName:paragraph}]; UILabel* myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 0, 0)]; myLabel.backgroundColor = [UIColor greenColor]; myLabel.attributedText = attrStr; [myLabel sizeToFit]; myLabel.numberOfLines = 2; … Read more

Change the color of a link in an NSMutableAttributedString

Swift Updated for Swift 4.2 Use linkTextAttributes with a UITextView textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.green] And in context: let attributedString = NSMutableAttributedString(string: “The site is www.google.com.”) let linkRange = (attributedString.string as NSString).range(of: “www.google.com”) attributedString.addAttribute(NSAttributedString.Key.link, value: “https://www.google.com”, range: linkRange) let linkAttributes: [NSAttributedString.Key : Any] = [ NSAttributedString.Key.foregroundColor: UIColor.green, NSAttributedString.Key.underlineColor: UIColor.lightGray, NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue ] // textView is a … Read more

Replace substring of NSAttributedString with another NSAttributedString

Convert your attributed string into an instance of NSMutableAttributedString. The mutable attributed string has a mutableString property. According to the documentation: “The receiver tracks changes to this string and keeps its attribute mappings up to date.” So you can use the resulting mutable string to execute the replacement with replaceOccurrencesOfString:withString:options:range:.