Convert attributed string, to, “simple” tagged html

According to the documentation of enumerateAttribute:inRange:options:usingBlock:, especially the Discussion part which states: If this method is sent to an instance of NSMutableAttributedString, mutation (deletion, addition, or change) is allowed, as long as it is within the range provided to the block; after a mutation, the enumeration continues with the range immediately following the processed range, … Read more

ios7 font size change when create nsattributedstring from html

I also faced this issue, I fixed it by iterating to the attributes and reseting the old font size as follows NSMutableAttributedString *res = [attributedText mutableCopy]; [res beginEditing]; [res enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, res.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) { if (value) { UIFont *oldFont = (UIFont *)value; UIFont *newFont = [oldFont fontWithSize:15]; [res addAttribute:NSFontAttributeName … Read more

NSAttributedString superscript styling

The following code seems to do the trick: UIFont *fnt = [UIFont fontWithName:@”Helvetica” size:20.0]; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@”GGG®GGG” attributes:@{NSFontAttributeName: [fnt fontWithSize:20]}]; [attributedString setAttributes:@{NSFontAttributeName : [fnt fontWithSize:10] , NSBaselineOffsetAttributeName : @10} range:NSMakeRange(3, 1)];

Looping Through NSAttributedString Attributes to Increase Font SIze

Something like this should work: NSMutableAttributedString *res = [self.richTextEditor.attributedText mutableCopy]; [res beginEditing]; __block BOOL found = NO; [res enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, res.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) { if (value) { UIFont *oldFont = (UIFont *)value; UIFont *newFont = [oldFont fontWithSize:oldFont.pointSize * 2]; [res removeAttribute:NSFontAttributeName range:range]; [res addAttribute:NSFontAttributeName value:newFont range:range]; found = YES; } … Read more

How to underline a UILabel in swift?

You can do this using NSAttributedString Example: let underlineAttribute = [NSAttributedString.Key.underlineStyle: NSUnderlineStyle.thick.rawValue] let underlineAttributedString = NSAttributedString(string: “StringWithUnderLine”, attributes: underlineAttribute) myLabel.attributedText = underlineAttributedString EDIT To have the same attributes for all texts of one UILabel, I suggest you to subclass UILabel and overriding text, like that: Swift 5 Same as Swift 4.2 but: You should prefer … Read more

UILabel and NSLinkAttributeName: Link is not clickable

I can answer my own question now: I am using UITextView instead of UILabel now. I have formatted the UITextView to look and behave like my labels and added: UITextView *textView = [[UITextView alloc] init]; textView.scrollEnabled = NO; textView.editable = NO; textView.textContainer.lineFragmentPadding = 0; textView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0); textView.delegate = self; Don’t forget … Read more

Example of NSAttributedString with two different font sizes?

You would do something like this… NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@”Presenting the great… Hulk Hogan!”]; [hogan addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20.0] range:NSMakeRange(24, 11)]; This will set the last two words in 20-point text; the rest of the string will use the default value (which I believe is 12 points). The thing that might be confusing about … Read more

Is it possible to change color of single word in UITextView and UITextField

Yes you need to use NSAttributedString for that, find the RunningAppHere. Scan through the word and find the range of your word and change its color. EDIT: – (IBAction)colorWord:(id)sender { NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:self.text.text]; NSArray *words=[self.text.text componentsSeparatedByString:@” “]; for (NSString *word in words) { if ([word hasPrefix:@”@”]) { NSRange range=[self.text.text rangeOfString:word]; [string addAttribute:NSForegroundColorAttributeName … Read more

Use multiple font colors in a single label

Reference from here. First of all initialize of you NSString and NSMutableAttributedString as below. var myString:NSString = “I AM KIRIT MODI” var myMutableString = NSMutableAttributedString() In ViewDidLoad override func viewDidLoad() { myMutableString = NSMutableAttributedString(string: myString, attributes: [NSFontAttributeName:UIFont(name: “Georgia”, size: 18.0)!]) myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:2,length:4)) // set label Attribute labName.attributedText = myMutableString super.viewDidLoad() } OUTPUT MULTIPLE … Read more

Extract UIImage from NSAttributed String

You have to enumerate the NSAttributedString looking for NSTextAttachments. NSMutableArray *imagesArray = [[NSMutableArray alloc] init]; [attributedString enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0, [attributedString length]) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) { if ([value isKindOfClass:[NSTextAttachment class]]) { NSTextAttachment *attachment = (NSTextAttachment *)value; UIImage *image = nil; if ([attachment image]) image = [attachment image]; else image = [attachment imageForBounds:[attachment bounds] … Read more