Attributed UITextView doesn't work with Korean symbols. Steps to reproduce:
Add UITextView to the form.
Use the following code:
NSDictionary *attributes = #{
NSFontAttributeName:[UIFont fontWithName:#"HelveticaNeue" size:15],
NSForegroundColorAttributeName:[UIColor blackColor]};
[textView setAttributes:attributes
range:NSMakeRange(0, textView.text.length)];
Run the application, type any text on Korean (에서 보냄) and tap or press Enter.
The Korean text will disappear or will be replaced by several trash symbols. Why? How can I fix it?
P.S. The is an interesting answer on the question UITextField text disappears on every other keystroke But I'm creating UITextView object on the code.
Use This code it will help you.
//[_txtViewChallangeDescription setBackgroundColor:[UIColor redColor]];
_txtViewChallangeDescription.font = [UIFont fontWithName:kFontHelvetica size:kFontSize14];
[_txtViewChallangeDescription setTextColor:[UIColor blackColor]];
[_txtViewChallangeDescription setEditable:NO];
_txtViewChallangeDescription.delegate=self;
_txtViewChallangeDescription.scrollEnabled=NO;
_txtViewChallangeDescription.textContainer.lineFragmentPadding = 0;
_txtViewChallangeDescription.textContainerInset = UIEdgeInsetsZero;
NSDictionary *attrDict = #{
NSFontAttributeName : [UIFont fontWithName:kFontHelvetica size:kFontSize14],
NSForegroundColorAttributeName :[UIColor colorWithRed:152.0/255.0f green:132.0/255.0f blue:43.0/255.0f alpha:1.0f]
};
[_txtViewChallangeDescription setLinkTextAttributes:attrDict];
The following incorrect code works fine:
NSDictionary *attributes = #{
NSFontAttributeName:[/*UIFont fontWithName:#"HelveticaNeue" size:15],*/
NSForegroundColorAttributeName:[UIColor blackColor]};
[textView addAttributes:attributes
range:NSMakeRange(0, textView.text.length)];
Related
I've been setting a UILabel using attributed text to get the font outlining that I want, and I change the UILabel's attributedText property fairly often. It seems that roughly 50% of the time it appears to render the new text OVER the old text without removing the old one. Right now my code looks like this:
// Attributes initialization
self.labelAttributes = [NSMutableDictionary dictionary];
[self.labelAttributes setObject: [UIFont fontWithName:#"Helvetica Neue" size:21] forKey: NSFontAttributeName];
[self.labelAttributes setObject: [UIColor whiteColor] forKey: NSForegroundColorAttributeName];
[self.labelAttributes setObject: [NSNumber numberWithFloat: -3.0] forKey: NSStrokeWidthAttributeName];
[self.labelAttributes setObject: [UIColor blackColor] forKey: NSStrokeColorAttributeName];
// Clear UILabel attributedString
self.userLabel.attributedText = [[NSAttributedString alloc] initWithString:#"" attributes:self.labelAttributes];
// Also attempted this with nil;
// Set UILabel to string, where self.userName and self.userAge are just regular strings.
NSString *labelString = [NSString stringWithFormat:#"%#, %#", self.userName, self.userAge];
self.userLabel.attributedText = [[NSAttributedString alloc] initWithString:labelString attributes:self.labelAttributes];
When it works, it looks like this:
When it doesn't work, it looks like this:
This appears to be the last user's name, plus the current user's name overlayed on top of each other.
I can't figure out a good way to guarantee the label is cleared, and am not sure how to debug it. (I've tried using visual debugging in XCode 6, but it still thinks it's just one label, with the new user's text as the text attribute.)
It should already be correct by default but you could try it anyway:
self.userLabel.clearsContextBeforeDrawing = YES;
self.userLabel.contentMode = UIViewContentModeRedraw;
Interesting. Don't really know why that isn't working. Your code seems fine. Only thing I can think of is that if this is a label in a table view cell and it being reset after it is offscreen and then on screen? Otherwise, no idea.
Here is another way of doing the same thing that works for me.
NSString *labelString = [NSString stringWithFormat:#"%#, %#", self.userName, self.userAge];
self.userLabel.text = labelString;
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithAttributedString:self.userLabel.attributedText];
[attStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Helvetica Neue" size:21] range:NSMakeRange(0, self.userLabel.length)];
[attStr addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, self.userLabel.length)];
[attStr addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithFloat:-3.0f] range:NSMakeRange(0, self.userLabel.length)];
[attStr addAttribute:NSStrokeColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, self.userLabel.length)];
//or 1 liner
//[attStr addAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:#"Helvetica Neue" size:21], NSFontAttributeName, [UIColor whiteColor], NSForegroundColorAttributeName, [NSNumber numberWithFloat:-3.0f], NSStrokeWidthAttributeName, [UIColor blackColor], NSStrokeColorAttributeName, nil] range:NSMakeRange(0, self.userLabel.length)];
[self.userLabel setAttributedText:attStr];
Also, I don't see why you just can't set the label to have those properties to begin with, (if they are not changing throughout the label).
You could just do:
NSString *labelString = [NSString stringWithFormat:#"%#, %#", self.userName, self.userAge];
self.userLabel.text = labelString;
self.userLabel.font = [UIFont fontWithName:#"Helvetica Neue" size:21];
self.userLabel.textColor = [UIColor blackColor];
//Then set the stroke with one attribute
Hope this helps! Happy coding.
You can do it as followed:
self.userLabel.attributedText = [[NSAttributedString alloc]
initWithString:#"string to both stroke and fill"
attributes:#{
NSStrokeWidthAttributeName: [NSNumber numberWithFloat:-3.0],
NSStrokeColorAttributeName: [UIColor blackColor],
NSForegroundColorAttributeName: [UIColor whiteColor]
}
];
I suspect that the problem is not related with the attributed text. I suspect that the problem is that you are adding a new UILabel in a reused UITableViewCell, instead of reuse the old UILabel.
I think I've seen this happen before with UITableCell reuse and dynamic table cell heights, determined at run time. This happens when cells are being set to height 0, if I remember correctly.
If you're in the above scenario, try turning clipsToBounds to on on the UITableViewCell, or the topmost reused view. Turning on clipsToBounds causes the labels, etc. to not flow out of the view when the containing view's frame size or height has been set to zero.
This question already has answers here:
UITextField attributedPlaceholder has no effect
(4 answers)
Closed 8 years ago.
I'm trying to format the placeholder text of a UITextField using an NSAttributedString. This code works successfully for the foreground color and kerning attributes. However, it won't change the font or the font size. What am I doing wrong? Thanks!
NSAttributedString *aString = [[NSAttributedString alloc] initWithString:
#"USER NAME"
attributes:#{
NSForegroundColorAttributeName: [UIColor redColor],
NSFontAttributeName : [UIFont fontWithName:#"Georgia" size:8.0f],
NSKernAttributeName : [NSNumber numberWithFloat:3.0f]
}];
self.userNameTextField.attributedPlaceholder = [aString copy];
For me it works when I set custom font for UITextField, and then set other attributes for placeholder. My example that works:
_searchField.font = [UIFont fontWithName:fontRokkitRegular size:20];
UIColor *color = [UIColor colorWithRed:150.0f/255 green:150.0f/255 blue:150.0f/255 alpha:1];
_searchField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Keyword search" attributes:#{NSForegroundColorAttributeName:color}];
It looks like the placeholder font size is governed by the UITextField's font so I think you will have to subclass UITextField and add:
-(void)drawPlaceholderInRect:(CGRect)rect
{
NSString *aString = #"USER NAME";
[aString drawInRect:rect withAttributes:#{NSForegroundColorAttributeName: [UIColor redColor], NSFontAttributeName : [UIFont fontWithName:#"Georgia" size:8.0f], NSKernAttributeName : [NSNumber numberWithFloat:3.0f]}];
}
- (IBAction)pinFieldChanged:(id)sender {
UITextField *pinField = sender;
float kerninig = 76.0;
NSAttributedString *attributedString =
[[NSAttributedString alloc]
initWithString:pinField.text
attributes:
#{
NSFontAttributeName : [UIFont fontWithName:#"Helvetica Neue" size:36],
NSForegroundColorAttributeName : [UIColor colorWithRed:130.0/255.0 green:130.0/255.0 blue:130.0/255.0 alpha:1.0],
NSKernAttributeName : #(kerninig)
}];
if ([pinField respondsToSelector:#selector(setAttributedText:)]) {
[pinField setAttributedText:attributedString];
}
}
My app is crashing under ios6.0 when i'm trying to set attributed text to text field, though this selector is available iOS 6.0 and later
Can you give me any idea why this could happen?
Thanks in advance!)
iOS5 may be expects a CTFont so use
NSString* s = #"Yo ho ho and a bottle of rum!";
NSMutableAttributedString* mas = [[NSMutableAttributedString alloc] initWithString:s];
__block CGFloat f = 18.0;
CTFontRef basefont = CTFontCreateWithName((CFStringRef)#"Baskerville", f, NULL);
https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/iOS5bookExamples/ch23p689styledText1/p579p593styleText1/RootViewController.m
use below ios 6
NSAttributedString *stringValue= [[NSAttributedString alloc] initWithString:#"cocoalibrary.blogspot.com" attributes:#{NSForegroundColorAttributeName: [UIColor redColor]}];
NSAttributedString is not available below than IOS 6. See this link, you can found this line.
In iOS 6 and later you can use attributed strings to display formatted
text in text views, text fields, and some other controls
I am having issues with retaining an attributed NSMutableString. I have a UITableView who's each UITableViewCell has an attributed text. Setting the attributed text is no problem, but upon selection, the UITableViewCell's attributes is lost. This is my code in cellForRowAtIndexPath that sets the attribute:
NSMutableAttributedString *changesStyleString_h = [[NSMutableAttributedString alloc] initWithString:#"Attributes change!" attributes:#{NSFontAttributeName: [UIFont boldSystemFontOfSize:20], NSForegroundColorAttributeName:[UIColor yellowColor]}];
[changesStyleString_h addAttributes:#{ NSUnderlineStyleAttributeName:#(1)} range:NSMakeRange(11, 6)];
cell.mainLabel.attributedText = changesStyleString
might i point out that mainLabel is also a UILabel, no customization there. Any help in the right direction would be greatly appreciated!
I found that I needed to set attributes on the ENTIRE string, or it would do funky things.
NSString* string = #"1 - some string"
NSMutableAttributedString* string = [[NSMutableAttributedString alloc] initWithString:string];
[string setAttributes:#{NSForegroundColorAttributeName: accent, NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue-Bold" size:13.f]} range:NSMakeRange(0, 1)];
This would cause weird behavior when highlighting the cell.
However, when I did this:
[string setAttributes:#{NSForegroundColorAttributeName: [UIColor blackColor]} range:NSMakeRange(1, [labelTwo length] - 1)];
Everything seemed to work as expected.
Hope that helps!
This example below is supposed to draw a stroke together with fill but it does not. What is wrong? I am using negative value to have stoke showed up according to Apple's documentation. If I make it a positive value, then text completely disappears.
UITextView *rte = [[UITextView alloc]initWithFrame:
CGRectMake(50,50,100,100)];
[self.view addSubview:rte];
NSDictionary *typingAttributes = #{
NSFontAttributeName: [UIFont fontWithName:#"Helvetica-Bold" size:20.0f],
NSForegroundColorAttributeName : [UIColor redColor],
NSStrokeColorAttributeName : [UIColor yellowColor],
NSStrokeWidthAttributeName : [NSNumber numberWithFloat:-2.0]
};
NSAttributedString *str = [[NSAttributedString alloc]
initWithString:#"Enter text here..."
attributes:typingAttributes];
rte.attributedText = str;
I have tried your code in iOS 7.0.3 simulator and got the result:
It is not working in iOS 6. I think it is a bug.
This approach is working
Sorry I don't have any explanation why in iOS 6 it doesn't work.