nsattributedstring

HTML with <blockquote> tags in UITextView

陌路散爱 提交于 2019-12-04 23:27:24
问题 I want to display HTML text with <blockquote> tags in the UITextView . example of HTML: <div> <blockquote class="uncited"> <div> <cite>Nick:</cite> <blockquote class="uncited"> <div> <cite>Tom:</cite>censored<br> Hello </div> </blockquote> <p>World</p> </div> </blockquote> <p>Text</p> </div> I use the following code to display HTML text in the cell: UITextView *textView = (UITextView*)[cell viewWithTag:100]; NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:

NSAttributedString '\\n' ignored

落花浮王杯 提交于 2019-12-04 23:09:39
I have a single view whose only UI element is a UITextView . In viewDidLoad: I create an attributed string with "Text\n" and set the text view's attributed text as such: NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"Text\n"]; [self.textView setAttributedText:string]; My problem is that the line break is ignored when I run the app. If I use an NSString and setText: that doesn't happen. NSString *string = [[NSString alloc] initWithString:@"Text\n"]; [self.textView setText:string]; Can anyone shed some light on what is happening? I can't seem to find anything in

Border around every word of UILabel

∥☆過路亽.° 提交于 2019-12-04 21:14:43
Is there a way i can draw border around every word of UILabel . Suppose UILabel contains the string " This is the Line 1". I want 5 different border around 5 words This Is the Line 1 VikingoS says Reinstate Monica I am not aware of an easy to use code for UILabel, but for UITextView: Swift playground setup: import UIKit let string = "Lorem ipsum dolor sit amet" let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) textView.text = string use regular expressions get a match for every word: let pattern = "[a-zA-Z0-9]+" let regex = try! NSRegularExpression(pattern: pattern,

UITextField underlines from NSAttributedString are only 1 pixel high?

旧城冷巷雨未停 提交于 2019-12-04 20:26:11
The label (bottom of the picture) and the text field (top) have the same same attributedText. But look at the underlines. The ones in the text field are only one pixel high. This looks terrible. Does anyone know what is causing this or how to prevent it? - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { UITextField* textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 600, 200)]; NSMutableAttributedString* string = [[NSMutableAttributedString alloc]initWithString:@"The quick brown fox jumps"]; NSNumber* underlineNumber = [NSNumber

How can I accurately detect if a link is clicked inside UILabels in Swift 4?

泄露秘密 提交于 2019-12-04 17:35:12
问题 Edit See my answer for a full working solution: I managed to solve this myself by using a UITextView instead of a UILabel . I wrote a class that makes the UITextView behave like a UILabel but with fully accurate link detection. I have managed to style the links without problem using NSMutableAttributedString but I am unable to accurately detect which character has been clicked. I have tried all the solutions in this question (that I could convert to Swift 4 code) but with no luck. The

Replace regex matches in attributed string with image in Objective-C

a 夏天 提交于 2019-12-04 17:08:24
My goal is to store the information for an attributed string in Parse.com. I decided to come up with an encoding for attributed text for my images that works by replacing any string {X} in braces with the corresponding image. For example: Picture of 2 colorless mana: {X} Should produce an attributed string where {X} is replaced by an image. This is what I've tried: NSString *formattedText = @"This will cost {2}{PW}{PW} to cast."; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=\\{)[^}]+(?=\\})" options:NSRegularExpressionAnchorsMatchLines error:nil];

How to set up a tap gesture only for specific ranges of a UILabel in Swift 3

假如想象 提交于 2019-12-04 16:32:24
I have an attributed string set to UILabel with multiple underlines , colors like below image and I know How to setup a tap gesture for whole label (with enabling user interaction ) and below is my code for what I have done including setting up underline and setting up font colors for multiple ranges . import UIKit class ViewController: UIViewController { @IBOutlet weak var mylabel: UILabel! var theString = "I have agree with the terms and conditions and privacy policy" override func viewDidLoad() { super.viewDidLoad() mylabel.text = theString let tap = UITapGestureRecognizer(target: self,

Adding strikethrough to NSAttributedString in iOS 11 with Swift

和自甴很熟 提交于 2019-12-04 16:09:22
Having some issues getting strikethrough to work. Currently I'm doing the following: theString.addAttributes([ NSAttributedStringKey.strikethroughStyle: NSUnderlineStyle.styleSingle.rawValue, NSAttributedStringKey.strikethroughColor: UIColor.white ], range: NSMakeRange(0, 1)) It's not showing any sort of strikethrough though. Any ideas? I can't seem to find anything that works. I use this to strike through a text in Xcode9/iOS11/Swift4 env let strokeEffect: [NSAttributedStringKey : Any] = [ NSAttributedStringKey.strikethroughStyle: NSUnderlineStyle.styleSingle.rawValue, NSAttributedStringKey

NSAttributedString initWithHTML incorrect character encoding?

a 夏天 提交于 2019-12-04 16:02:06
问题 -[NSMutableAttributedString initWithHTML:documentAttributes:] seems to mangle special characters: NSString *html = @"“Hello” World"; // notice the smart quotes NSData *htmlData = [html dataUsingEncoding:NSUTF8StringEncoding]; NSMutableAttributedString *as = [[NSMutableAttributedString alloc] initWithHTML:htmlData documentAttributes:nil]; NSLog(@"%@", as); That prints “Hello†World followed by some RTF commands. In my application, I convert the attributed string to RTF and display it in an

iOS: Add Button over NSAttributedString

怎甘沉沦 提交于 2019-12-04 15:31:15
I want to know if is possible to put a UIButton over a exactly coordinate of a piece of text. For example, my UILabel have this text: "Dont have an account? Sign In " In the italic content, I need to put a button over then, the touch over this button will trigger an action that will open a Sign In View Is very important to make this feature well, to work with same in other languages without do anything :) Thanks I'll respond my answer because this can be helpful for other developers that got the same problem. 1 : You need to implement UITextViewDelegate method: - (BOOL)textView