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
Stanford Calculator App Keeps Crashing
Bulk Fix Hundreds of "#Selector Not Exposed to Objective-C" Errors in Xcode 9 or 9.1
How to Move a Model and Generate Its Collision Shape at the Same Time
Accessing and Manipulating Array Item in an Environmentobject
Firestore Security Rules Breaking with Update Rule
Cfdictionary Get Value for Key in Swift3
How to Make a Function with a Loop Asynchronous in Swift
Randomizing Node Movement Duration
How to Group an Array of Image Urls Together in Firebase Realtime Database? (Swift)
How to Use a Specific Gmt for a Function Which Will Be Recognised by Other Time Zones
How to Create Lazy Combinations
Xcode Gm: No Swift Language for Os X Command Line Tool Project
Linking Error When Building Parse in Xcode 7