Composing NSAttributedString with SwiftUI-style syntax - ethanhuang13/NSAttributedStringBuilder

seen from Russia
seen from China
seen from Türkiye
seen from China
seen from United States
seen from Germany
seen from Brazil

seen from Australia
seen from China

seen from United States
seen from Japan

seen from Russia
seen from United States
seen from Russia
seen from T1
seen from China
seen from United States
seen from Malaysia
seen from Canada

seen from Russia
Composing NSAttributedString with SwiftUI-style syntax - ethanhuang13/NSAttributedStringBuilder

Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
Free to watch • No registration required • HD streaming
Convert NSRange from Range before Swift 4
let str = "Hello, playground! Hello, world! Hello, earth!" let attributedString = NSAttributedString(string: str) attributedString.enumerateAttributes(in: NSRange(location: 0, length: str.characters.count), options: NSAttributedString.EnumerationOptions.longestEffectiveRangeNotRequired) { (attributes, range, stop) in }
In order to have a smoother migration from now to Swift 4. Let's create a grammar compatible function to convert NSRange from Range before Xcode 9 is released officially.
extension NSRange { init(_ range: Range<String.Index>, in string: String) { self.init() let startIndex = range.lowerBound.samePosition(in: string.utf16) let endIndex = range.upperBound.samePosition(in: string.utf16) self.location = string.distance(from: string.startIndex, to: range.lowerBound) self.length = startIndex.distance(to: endIndex) } }
And now, we are able to do conversion like this NSRange(range, in: "A string"). Let's do an experiment. The following processes are trying to convert range0: Range -> range1: Range -> range2: NSRange.
let str = "Hello, playground! Hello, world! Hello, earth!" let range0 = str.range(of: "playground") print(String(describing: type(of: range0))) let range1 = NSRange(range0!, in: str) print(String(describing: type(of: range1))) let start = range1.location let end = start + range1.length let range2 = Range(uncheckedBounds: (start, end)) print(String(describing: type(of: range2)))
Attributed String for iOS in Swift
I have been talking quite a lot in the past about how to customize text in your app to improve the UI of your applications. Before iOS 6, Core Text was the only available option for developers. Although a great framework, Core Text is not a very straightforward tool to use. You can check our Core Text tutorial in Objective-C here. In iOS 6, Apple introduced NSAttributedString. You can check a couple of posts about this topic here and here.
Keep reading
Fixed How do you use NSAttributedString? #dev #it #asnwer
Fixed How do you use NSAttributedString? #dev #it #asnwer
How do you use NSAttributedString?
Multiple colours in an NSString or NSMutableStrings are not possible. So I’ve heard a little about the NSAttributedString which was introduced with the iPad SDK 3.2 (or around 3.2) and is available on the iPhone as of iPhone SDK 4.0 beta.
I would like to have a string that has three colours.
The reason I don’t use 3 separate NSStrings, is because the length of…
View On WordPress

Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
Free to watch • No registration required • HD streaming
How to: Bold & Non-Bold Text In A Single UILabel?
How to: Bold & Non-Bold Text In A Single UILabel?
Bold & Non-Bold Text In A Single UILabel?
How would it be possible to include both bold and non-bold text in a uiLabel?
I’d rather not use a UIWebView.. I’ve also read this may be possible using NSAttributedString but I have no idea how to use that. Any ideas?
Apple achieves this in several of their apps; Examples Screenshot:
Thanks! – Dom
Answer: Bold & Non-Bold Text In A Single UILabel? Update…
View On WordPress
How to: How do you use NSAttributedString?
How do you use NSAttributedString?
Multiple colours in an NSString or NSMutableStrings are not possible. So I’ve heard a little about the NSAttributedString which was introduced with the iPad SDK 3.2 (or around 3.2) and is available on the iPhone as of iPhone SDK 4.0 beta.
I would like to have a string that has three colours.
The reason I don’t use 3 separate NSStrings, is because the length of…
View On WordPress
Touchable text in NSMutableAttributedString
Do you want some text clickable in your text string? That's easy using iOS7/8, NSMutableAttributedString and the UITextView control!
1- Set text into the UITextView. Clickable texts must be a custom attribute;
//ViewDidLoad NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init]; paragraph.alignment = NSTextAlignmentJustified; paragraph.lineSpacing = 3; NSMutableAttributedString* agreeAttributedString = [[NSMutableAttributedString alloc] initWithString:@"You confirm you read our {0} and our {1}" attributes:@{ NSForegroundColorAttributeName : [UIColor blackColor]}]; NSAttributedString* termsAttributedString = [[NSAttributedString alloc] initWithString:@"Terms" attributes:@{ @"termsTag" : @(YES), NSForegroundColorAttributeName : [UIColor redColor]}]; NSAttributedString* policyAttributedString = [[NSAttributedString alloc] initWithString:localize:@"Policy" attributes:@{ @"policyTag" : @(YES), NSForegroundColorAttributeName : [UIColor greenColor]}]; NSRange range0 = [[agreeAttributedString string] rangeOfString:@"{0}"]; if(range0.location != NSNotFound) [agreeAttributedString replaceCharactersInRange:range0 withAttributedString:termsAttributedString]; NSRange range1 = [[agreeAttributedString string] rangeOfString:@"{1}"]; if(range1.location != NSNotFound) [agreeAttributedString replaceCharactersInRange:range1 withAttributedString:policyAttributedString]; [agreeAttributedString addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, [agreeAttributedString length])]; self.txvText.attributedText = agreeAttributedString;
2- Add gesture tap to the UITextView
//ViewDidLoad UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(txvTextTouched:)]; [self.txvText addGestureRecognizer:tap];
3- Recognize the tap, find the character that's been tapped and check its attributes
-(void)txvTextTouched:(UITapGestureRecognizer *)recognizer { UITextView *textView = (UITextView *)recognizer.view; NSLayoutManager *layoutManager = textView.layoutManager; CGPoint location = [recognizer locationInView:textView]; location.x -= textView.textContainerInset.left; location.y -= textView.textContainerInset.top; NSUInteger characterIndex; characterIndex = [layoutManager characterIndexForPoint:location inTextContainer:textView.textContainer fractionOfDistanceBetweenInsertionPoints:NULL]; if (characterIndex < textView.textStorage.length) { NSRange range0; NSRange range1; id termsValue = [textView.textStorage attribute:@"termsTag" atIndex:characterIndex effectiveRange:&range0]; id policyValue = [textView.textStorage attribute:@"policyTag" atIndex:characterIndex effectiveRange:&range1]; if(termsValue) { NSLog(@"TERMS TAPPED"); return; } if(policyValue) { NSLog(@"POLICY TAPPED"); return; } } }
That's all!
NSLog(@"%@, %lu, %lu", termsValue, (unsigned long)range0.location, (unsigned long)range0.length);