Change text parameters in UITextView - objective-c

I am trying to display some text in a UITextView (no editable) and I am having some problems to change font size, color...
I am using xcode 5 with ios6 appearance. I tried to change parameters in nib file: arribute inspector> text view.
But all I try, seems to do nothing. Nothing changes, I see the text equal as if I didn't modify anything.
I don't write text inside uitextview, I just show it from a variable.

I had the same problems and had already an answer here
You need to set the text of your UITextView and after you can set the font and the color :
_myTextView.text = #"text";
[_myTextView setFont:[UIFont fontWithName:#"Helvetica Neue" size:18.0f]];
_myTextView.textColor = [UIColor whiteColor];

I had a similar problem before. It got fixed when I did the customization after selecting the entire default text.
Here is a sample screenshot :
Hope this helps !

Related

Changing Font in UITextView does not work in iOS7

I have a UITextView with text in it, the View is in a UITableViewCell. I noticed that the font was not quite the same on iOS7 as with iOS6, noting it was set to "system" I decided to specify the exact font/size.
It appeared nothing happened so I thought I would do a better test (big font not used anywhere), like this in my "CellForRowAt....";
cell.newsItemDescription.font = [UIFont fontWithName:#"didot" size:20];
cell.newsItemDescription.text = newsDescriptions[indexPath.row];
In iOS6 it comes out like this;
In iOS7 it comes out like this;
It happens in just a few places in the app but it is very annoying, can't figure out why? I am fast getting to the point where I may use the iOS7 Font/ Size throughout the app.
Some extra info;
The UITextView is resized per cell along with the cell (using springs/struts, i.e. no Auto Layout) and HeightForRow...
The font was setup in Storyboard originally (as system)
This is the same on devices and Simulator
I have a strange behavior in iOS 7. Font is smaller than I expect, if I was set it in to the xib.
If I set font after setting the text it's works for me. Otherwise font is smaller.
Try this:
cell.newsItemDescription.text = newsDescriptions[indexPath.row];
cell.newsItemDescription.font = [UIFont fontWithName:#"didot" size:20];
I had the same issue today with UITextView, and while reading AlKozin's answer, I remembered something: somewhere, sometime I read that since iOS 7, the best practice to set font styles is to set them after the View has loaded. In other words, if I set everything in viewDidLoad: , nothing happens. You have to set up the font of your UITextView in viewWillAppear:, like this:
-(void)viewWillAppear:(BOOL)animated
{
self.myTextView.font = [UIFont fontWithName:#"Baskerville-Italic" size:18.0];
}
I ran into a weird bug (Xcode 7.2.1), where unchecking "Selectable" in IB was causing the UITextView to not adhere to the font settings specified through IB.
This is same issue I was facing.
Making UITextview "selectable" from storyboard will work.
Setting textview.font worked fine for me on iOS 7, but none of these answers, or other similar answers on other SO pages worked for me on iOS 8. The only way I was able to get it working was to use an NSAttributedString.
NSMutableParagraphStyle *pStyle = [[NSMutableParagraphStyle alloc] init];
[pStyle setAlignment:NSTextAlignmentCenter];
tv.attributedText = [[NSAttributedString alloc]
initWithString:text
attributes:#{ NSForegroundColorAttributeName: color,
NSFontAttributeName: [UIFont systemFontOfSize:fontSize],
NSParagraphStyleAttributeName: pStyle,
} ];

UILabel gets hidden behind UIImageView

I have made a subclass for UITableViewCell and I am implementing Subtitle TableViewCell with a thumbnail image.
Following are the UITableViewCell contents:
The issue I am facing is when the data loads in TableViewCell, the subtitleLabel text gets hidden upto the height of the imageView. But when I select any Cell, it shows subtitleLabelText completely.
I have added the screenshot of the same for complete reference:
The UIImageView has frame = CGRectMake(0,0,40,40);
Try to give a clearColor background color for the cell title label -
cell.textLabel.backgroundColor = [UIColor clearColor];
It turns out I was using TableViewCell style as subtitle instead of custom. The style settings in subtitle was making the other labels to hide below them. What a silly miss!
In your nib or storyboard file, make sure that the label is below the image view in the list of subview components (it is in the left of the screen). The first subview in that list will be at the lowest level (behind every other subview, if they overlap).
write one line of code
Titlelabel.backgroundcolor = [UIColor ClearColor];
because your label has white background..and Titlelabel height is too large so label is colliding.
Let me know working or not!!!
Happy Coding!!!
What is the frame of Title Label? if its height is more, then also it may possible that it hides your subtitle Label
Here's a great tutorial which helped me when I was trying to do something like you want :
http://www.appcoda.com/ios-programming-customize-uitableview-storyboard/
You can adapt the size of the different components (ImageView, TitleLabel, Subtitle,...)

NSTextView color and font reset after clearing it

I set some default font and color to a NSTextView inside of a NSPanel.
However, when I clear the view with setString:#"", not only does the text disappears, but all the default colors/fonts formatting.
After I do another setString, the text becomes the default font and black again.
Can someone explain why that is and what can I do to remedy it?
UPDATED:
Thanks for the help and clarification of the issue.
I ended up just formatting it again after I setString.
[self.txtLog setFont:[NSFont fontWithName:#"courier" size:12]];
[self.txtLog setTextColor:[NSColor colorWithSRGBRed:65.0/255 green:229.0/255 blue:235.0/255 alpha:1]];
It is because clearing the NSTextView and setting a regular (non-attributed) string removes the formatting, which includes font and color.
Most of the time, I set attributed strings that I format using HTML. I find it to be very convenient. For instance, here are two macro I use to set the color and the font:
#define color(string, color) strAdd5(#"<font color=\"#",(color), #"\">", (string), #"</font>")
#define fontName(string, fontname) strAdd5(#"<span style=\"font-family: ",(fontname), #";\">", (string), #"</span>")
#define html2AttributedString(htmlString) [[[NSAttributedString alloc] initWithHTML:[(htmlString) dataUsingEncoding:NSUTF8StringEncoding]documentAttributes:NULL] autorelease]
Then, all I need is to set the attributed string in the text storage:
[textStorage setAttributedString:html2AttributedString(color(myNSString, #"FF0000"))]
or to use insertText of NSTextView
[textView insertText:html2AttributedString(fontName(myNSString, #"Courier New"))];
And using HTML, you can define everything including centering, and many other properties. But, you have to convert any new lines into < BR / > before converting the text.

Setting size to custom iOS font doesnt work

I am using some custom fonts in my iphone app. They are displayed correctly, but i cant change their size.
Here is how set this font for UILabel:
myLabel.font=[UIFont fontWithName:#"Cabin-Regular" size:18];
This label is part of uitableview cell, if that could play any role.
Label settings in IB:
I change only color, text and font in code for this label.
What could be wrong?
Are you sure the font is loaded correctly?
You can try:
for (NSString *familyName in [UIFont familyNames]) {
NSLog(#"%#", familyName);
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(#"\t%#",fontName);
}
}
This will show you all the fonts that are loaded. Make sure the same name "Cabin-Regular" is showing up exactly the same. A lot of times the font itself isn't loaded and then the font-size won't be used at all.
You need to add your font in your Xcode, if you are adding manually.
Add your font in your project files
Go to your Project name-Info.plist file, right click and click Add Row.
In your new row type Fonts provided by application. If you expand it you can add new items, in there add value to item 0 . The value should be your font file name. like below image
4. Double click on your font, it'll open in external editor, like following image
5.Give that name into your fontWithName: method. If you are setting value to your tableviewcell. the code will be
cell.nameLbl.font = [UIFont fontWithName:#"Cabin" size:18.0];
It works fine for me. I hope it helps :)

UITextView setTextColor changes layout in UITableViewCell

I am trying to use the solution on the following page:
UITextView highlightedTextColor or similar option?
However, I am finding that when I call setTextColor on my UITextView and set the color to anything other than Color blackColor, the content in the UITextView appears shifted, and the color I do set it to doesn't take.
Playing around, I can repeat this behaviour by modifying the following initialization code in my UITableViewCell:
_notesTextView = [[UITextView alloc] initWithFrame:CGRectZero];
[_notesTextView setFont:[UIFont systemFontOfSize:12.0]];
[_notesTextView setTextColor:[UIColor redColor]];
[_notesTextView setUserInteractionEnabled:NO];
[self.contentView addSubview:_notesTextView];
The code above will cause the textView to display offset from what I expect, as compared to when I leave the color defaulted or set to blackColor, and the text doesn't show as red either.
This is so weird - any idea what could be wrong?
Do Not Use CGRectZero as it initiates the frame to some value you might know depending upon situation so if you have created a custom UITableViewCell then just set the frames in init or
-(void)layoutSubViews
{
}
I've confirmed that the textColor does indeed shift the text position (up and down for me). I've also tried it with initWithFrame(50, 20, 250, 31), and still experience the same problem.
What I've discovered fixes this issue is using the property textAlignment and setting it to one of the values: UITextAlignmentCenter, UITextAlignmentLeft, UITextAlignmentRight.