UITextView renders custom font incorrectly in iOS 7 - ios7

My app uses a UITextView to input Syriac text (Estrangelo font), but UITextView renders some letters incorrectly like this:
I tested it with a UILabel and a UITextView. UILabel displays it correctly, but UITextView incorrectly displays the top dots and moves them to the bottom (see the above result).
This problem only occurs in iOS 7 and does not occur in iOS 6. Please tell me if there's any way to fix the problem.
This is my test code
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
label.center = CGPointMake(self.view.center.x, self.view.center.y-40);
label.font = [UIFont fontWithName:#"East Syriac Adiabene" size:24];
label.text = #"ܩ̈ ܡ̄ ܬ̇ ܒ̃";
[self.view addSubview:label];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
textView.center = CGPointMake(self.view.center.x, self.view.center.y+40);
textView.font = [UIFont fontWithName:#"East Syriac Adiabene" size:24];
textView.text = #"ܩ̈ ܡ̄ ܬ̇ ܒ̃";
[self.view addSubview:textView];

I debugged this issue a little, and it seems to be a bug in the way NSLayoutManager layouts the text. As other answers pointed out, UITextView is build around TextKit since iOS7, and thus uses NSLayoutManager internally to layout text. UILabel uses Core Text to layout text directly. Both eventually use Core Text to render the glyphs.
You should open a bug report with Apple and post the number so people can duplicate it. The issue has not been fixed so far in iOS7.1 betas.
As a workaround, you can replace UITextView with other Core Text alternative editors, which layout and render directly with Core Text, where the issue does not exist.
I tested SECoreTextView, and it shows the text correctly. It implements a similar API to UITextView but internally uses Core Text.
Here is how it looks after swapping UITextView with SECoreTextView:
SETextView *textView = [[SETextView alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
textView.center = CGPointMake(self.view.center.x, self.view.center.y+40);
textView.font = [UIFont fontWithDescriptor:desc size:24];
textView.text = #"ܩ̈ ܡ̄ ܬ̇ ܒ̃";
textView.textColor = [UIColor blackColor];
textView.backgroundColor = [UIColor whiteColor];
textView.editable = YES;
[self.view addSubview:textView];

Some days ago, I had the same problem as yours in iOS 7 (but the font was different).
I set the FONT after setting the TEXT and it worked for me. So for your case:
textView.text = #"ܩ̈ ܡ̄ ܬ̇ ܒ̃"; // Setting the text.
textView.font = [UIFont fontWithName:#"East Syriac Adiabene" size:24]; // Setting the font and it's size.
It may seem silly, but it worked for me.
Also see this question/answer. There are many tips for using custom fonts with UITextView, which may be helpful for you.
EDIT :
iOS 7 also introduced a new selectable property on the UITextView for enabling text selection. So make sure you have done the following:
self.textField.editable = YES;
self.textField.selectable = ON;

Related

Position UILabel in area relative to screen resolution

I am trying to position UILabel relative to screen resolution (iPhone v iPad) so that the UILabel does not interfere with splash screen graphics at start-up. When the app was iPhone only, the label was located properly. Once the app was made universal, the Label interfered with the image on iPad (of course)
I am using the method below, which works fine, but it is not very forward thinking in terms of new devices and/or new screen resolutions.
Can anyone suggest a more efficient way to display the UILabel "Connecting to Server..." within the area circled in red on the attached image at the link below (I do not have auth to post images here yet)?
UILabel *loadingLabel;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 350, self.window.frame.size.width, 20)];
}
else
{
loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 700, self.window.frame.size.width, 20)];
}
loadingLabel.text = #" Connecting to Server...";
loadingLabel.textAlignment = UITextAlignmentCenter;
loadingLabel.textColor = [UIColor whiteColor];
loadingLabel.backgroundColor = [UIColor clearColor];
Splash Screen
You can use the autoresizingMask property to accomplish it.
set your origin.y on the label to be self.window.bounds.size.height - 200 or so. Then set the autoresizingMask to be UIViewAutoresizingMakFlexibleBottomMargin

Custom font in Navigation Bar Truncating uses semicolon not ellipses

I am trying to customize the font on my Navigation Bar. Everything is fine except for some reason, when the title is too long for the Navigation Bar, it gives me a semicolon instead of ellipses (...)
How do I get the ... to show that the title is cut off?
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 240, 24)];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:#"My Font" size:20.0];
label.numberOfLines = 1;
label.lineBreakMode = NSLineBreakByTruncatingTail;
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor whiteColor];
label.text=#"Some Really Long Title Name That's Normally Dynamic";
self.navigationItem.titleView = label;
So I want the Navigation Bar to say "Some Really Long Titl..." However it keeps saying "Some Really Long Title ;"
Thanks!
The reason is that the '...' glyph you see is that, one unicode glyph. The font you are using either defines that incorrectly, or it does not define it at all. If you have some font editing tools, you can probably fix it but I don't know how. That character can be 'typed' on a Mac using option-;, probably why you see the ';'.

Custom navbar titleView won't clear previous titles before loading new ones

So I created a custom UINavigationItem category to be able to make a custom titleview for my navbar, but everytime I push/pop a view, it simply adds the new title without getting rid of the old one causing the title to just be a jumble of letters. Here's the relevant code:
#implementation UINavigationItem (CustomNavigationItem)
-(UIView *)titleView
{
[self setTitleView:nil];
UILabel *newTitleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 220, 32)];
newTitleView.center = CGPointMake(160, 22);
newTitleView.backgroundColor = [UIColor clearColor];
newTitleView.textColor = [UIColor whiteColor];
newTitleView.textAlignment = UITextAlignmentCenter;
newTitleView.text = self.title;
newTitleView.textAlignment = UITextAlignmentCenter;
return newTitleView;
}
#end
You have to remove the old uilabel from its superview, by setting to nil it doesn't do that. That's why you are messing the letters on screen. I also do not think you are getting a recursion, because you are caling the setter, but I maybe wrong.
A quick thing you could is to assign a tag to your newest created view.
[[self.view viewWithTag:YourCustomEnumTag] removeFromSuperView];
// create your view....
textView.tag=YourEnumCustomTag;

UILabel sizeWithFont: problem. Clipping italic text

I have created a UILabel that displays a single large character. Even with clipsToBounds = NO; I still get clipping.
See link: http://img341.imageshack.us/img341/5310/screenshot20100814at243.png
I used the following code:
CGSize fBounds = [myLabel.text sizeWithFont:cFont];
To get what should be the bounding rectangle of the font. And the label is drawn with:
myLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 300)];
myLabel.clipsToBounds = NO;
myLabel.numberOfLines = 1;
myLabel.textAlignment = UITextAlignmentCenter;
myLabel.adjustsFontSizeToFitWidth = YES;
myLabel.minimumFontSize = 10;
myLabel.text = #"A";
myLabel.font = [UIFont fontWithName:#"CourierNewPSMT" size:300];
myLabel.textColor = [UIColor blackColor];
myLabel.backgroundColor = [UIColor colorWithRed:1 green:.5 blue:0 alpha:.5];
In the image below, the size returned from sizeWithFont is rendered by the semi-transparent blue rectangle overlay. As you can see, with an italic font (in this case Verdana-BoldItalic), the character extends past what sizeWithFont returns. Further, the UILabel's frame (the orange color) also clips the character. Thoughts? Maybe I could override some text drawing routine. Also, not sure if this is the same problem as here:
UIButton.titleLabel clipping text problem
Use attributed text + indent...
Looks like this is an apple problem. Ended up doing custom drawing with CoreText.

How to programmatically add text to a UIView

I have a UIView that I'd like to add several bits of text to. I have used a UITextView but I think that's overkill as it doesn't need to be editable. I thought about using a UILabel or a UITextField, but I don't see how you tell the superview where to position the UILabel or UITextField within itself. I want the lowest footprint object that will let me put text of a font/color/size of my choosing in my UIView where I want it. Not too much to ask, eh?
The simplest approach for you would be:
UILabel *yourLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 20)];
[yourLabel setTextColor:[UIColor blackColor]];
[yourLabel setBackgroundColor:[UIColor clearColor]];
[yourLabel setFont:[UIFont fontWithName: #"Trebuchet MS" size: 14.0f]];
[yourSuperView addSubview:yourLabel];
Building or populating Views in your code will probably require you to use CGRectMake a lot.
As its name says, it creates a rectangle that you can use to set the relative position (relative to the borders of your superview) and size of your UIView-Subclass (in this case a UILabel).
It works like this:
yourLabel.Frame = CGRectMake(x, y, width, height); //x,y,width,height are float values.
x defines the spacing between the left hand border of the superview and the beginning of the subview your about to add, same applies to y but relating to the spacing between top-border of your superview.
then width and height are self-explanatory i think.
Hope this gets you on the track.
Instead of finding a way to tell the view where to position the UILabel, you can tell the UILabel where to position itself in the view by using "center".
E.g.
myLabel.center = CGPointMake(0.0, 0.0);
Hope you'll be able to use UILabel, for me it's the basic form of a flexible non editable text.
For Swift:
let yourLabel = UILabel(frame: CGRectMake(100, 100, 100, 100))
yourLabel.textColor = UIColor.whiteColor()
yourLabel.backgroundColor = UIColor.blackColor()
yourLabel.text = "mylabel text"
yoursuperview.addSubview(yourLabel)
This question is old, but for a pure UIView text option without using UILabel or UITextField (as all the other answers describe, but the question is how to do it without them), drawRect in a subclassed UIView works for me. Like so:
- (void)drawRect:(CGRect)rect{
NSString *string = #"Hello World!";
[string drawAtPoint:CGPointMake(100, 100) withFont:[UIFont boldSystemFontOfSize:16.0]];
}
This routine displays a text at a X-Y position
-(void)placeText:(NSString *)theText:(int)theX:(int)theY {
UILabel *textLabel;
// Set font and calculate used space
UIFont *textFont = [UIFont fontWithName:#"Helvetica" size:14];
CGSize textStringSize = [theText sizeWithFont:textFont constrainedToSize:CGSizeMake(300,50) lineBreakMode:NSLineBreakByTruncatingTail];
// Position of the text
textLabel = [[UILabel alloc] initWithFrame:CGRectMake(theX+OFFSETIMAGEX-(textStringSize.width/2), theY+OFFSETIMAGEY-(textStringSize.height/2), textStringSize.width,textStringSize.height)];
// Set text attributes
textLabel.textColor = [UIColor blackColor];
textLabel.backgroundColor = [UIColor orangeColor];
textLabel.font = textFont;
textLabel.text = theText;
// Display text
[self.view addSubview:textLabel];
}
It might be late but here is what I use:-
CGRect labelFrame = CGRectMake(120,300, 530, 100);
UILabel *myLabel = [[UILabel alloc] initWithFrame:labelFrame];
//If you need to change the color
[myLabel setTextColor:[UIColor whiteColor]];
//If you need to change the system font
[myLabel setFont:[UIFont fontWithName:NULL size:23]];
//If you need alignment
[myLabel setTextAlignment:NSTextAlignmentCenter];
// The label will use an unlimited number of lines
[myLabel setNumberOfLines:0];
//Add label view to current view
[self.view addSubview:myLabel];
NSString *someString = #"Sample String, Yarp!";
myLabel.text = someString;
add a UILabel to your View. then override the View's layoutSubviews method.