Toggle Selectedrange Attributes in Uitextview

Toggle selectedRange attributes in UITextView

This might do the trick:

func toggleBold() {
if let textRange = selectedRange {

let attributedString = NSAttributedString(attributedString: noteContents.attributedText)

//Enumerate all the fonts in the selectedRange
attributedString.enumerateAttribute(.font, in: textRange, options: []) { (font, range, pointee) in
let newFont: UIFont
if let font = font as? UIFont {
if font.fontDescriptor.symbolicTraits.contains(.traitBold) { //Was bold => Regular
newFont = UIFont.systemFont(ofSize: font.pointSize, weight: .regular)
} else { //Wasn't bold => Bold
newFont = UIFont.systemFont(ofSize: font.pointSize, weight: .bold)
}
} else { //No font was found => Bold
newFont = UIFont.systemFont(ofSize: 17, weight: .bold) //Default bold
}
noteContents.textStorage.addAttributes([.font : newFont], range: textRange)
}
}
}

We use enumerateAttribute(_:in:options:using:) to look for fonts (since bold/non-bold) is in that attribute.
We change it according to your needs (bold <=> unbold).

Toggle selectedRange attributes in UITextView

This might do the trick:

func toggleBold() {
if let textRange = selectedRange {

let attributedString = NSAttributedString(attributedString: noteContents.attributedText)

//Enumerate all the fonts in the selectedRange
attributedString.enumerateAttribute(.font, in: textRange, options: []) { (font, range, pointee) in
let newFont: UIFont
if let font = font as? UIFont {
if font.fontDescriptor.symbolicTraits.contains(.traitBold) { //Was bold => Regular
newFont = UIFont.systemFont(ofSize: font.pointSize, weight: .regular)
} else { //Wasn't bold => Bold
newFont = UIFont.systemFont(ofSize: font.pointSize, weight: .bold)
}
} else { //No font was found => Bold
newFont = UIFont.systemFont(ofSize: 17, weight: .bold) //Default bold
}
noteContents.textStorage.addAttributes([.font : newFont], range: textRange)
}
}
}

We use enumerateAttribute(_:in:options:using:) to look for fonts (since bold/non-bold) is in that attribute.
We change it according to your needs (bold <=> unbold).

How to change some text colors in multiple colored UITextView

Use method enumerateAttribute:inRange:options:usingBlock:

Example Obj-C:

NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:@"Test Attributed String"];
[string addAttribute:NSStrokeColorAttributeName value:[UIColor redColor] range:NSMakeRange(3, string.length-3)];
[string enumerateAttribute:NSStrokeColorAttributeName inRange:NSMakeRange(0, string.length) options:NSAttributedStringEnumerationReverse usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
// value is color
}];

Replace UITextViews text with attributed string

I am not sure that this is correct solution, but it works.

Just disable scrolling before formatting text and enable it after formatting

- (void)formatTextInTextView:(UITextView *)textView
{
textView.scrollEnabled = NO;
NSRange selectedRange = textView.selectedRange;
NSString *text = textView.text;

// This will give me an attributedString with the base text-style
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];

NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"#(\\w+)" options:0 error:&error];
NSArray *matches = [regex matchesInString:text
options:0
range:NSMakeRange(0, text.length)];

for (NSTextCheckingResult *match in matches)
{
NSRange matchRange = [match rangeAtIndex:0];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:matchRange];
}

textView.attributedText = attributedString;
textView.selectedRange = selectedRange;
textView.scrollEnabled = YES;
}


Related Topics



Leave a reply



Submit