I have two lined Label and setting the backgroundColor with an attributedString.
NSString *string = #"This is a UILAbel with two lines";
NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc]initWithString:
string];
[mutableString setAttributes:#{NSBackgroundColorAttributeName : [UIColor redColor]} range:NSMakeRange(0, string.length)];
If the text makes a line break it fills the backgroundColor to the right border and in the second line it stops at the last character.
But I want the backgroundcolor in the first line just set to the last character too.
The backgroundColor property is applied to the entire rect representing the label.
To resolve this you need to dynamically adjust your UILabel's frame depending on its content, in order to achieve the desired effect.
There is a method on UILabel called sizeToFit; try calling it.
Although I recommend you use autolayout and let the label resize itself based on constraints.
Related
I have created a static table view in Storyboard with one cell that contains a UITextField for text entry. The text field has constraints to bind it to the cell for top, bottom, trailing, and leading so that it will have the right width on all resolutions.
In viewDidLoad, I enter some placeholder text for an empty state.
NSMutableAttributedString *placeholderString =
[[NSMutableAttributedString alloc]
initWithString:NSLocalizedString(#"Note", #"")];
[placeholderString beginEditing];
[placeholderString addAttribute:NSForegroundColorAttributeName value:
[PulseColor tableViewEmptyCellBackgroundColor] range:NSMakeRange(0,
placeholderString.length)];
[placeholderString endEditing];
self.txtNote.attributedPlaceholder = placeholderString;
self.txtNote.delegate = self;
The problem is that rather that sticking to the constraints, the width of the UITextField shrinks to the size of the placeholder text. As I type and the message reaches the end of the smaller text field, the text expands to the left and disappears before expanding to the right. (If I had the reputation, I'd post images)
Is there a setting that I am missing or some other way that will either cause the UITextField to keep its width across the cell, or cause the text to not disappear to the left as it reaches the initial length?
I have 3 UILabels stacked on top of each other like so:
The top label can be 2 lines, but isn't all of the time. I'm trying to vertically center all 3 labels between the top and bottom lines in picture. I'm just not sure how to go about doing it. I've tried setting the space between them equal and give ≥ margins on the top and bottom but it doesn't seem to work.
Trying to do this without code, all in the interface builder if possible.
You could try putting the three labels inside a UIView, and then assigning that UIView to align to the center of the UIImageView.
You can easily do this by selecting all three UILabels, and going to Editor->Embed In->UIView. Select the new view and Shift Click on the image and select Editor->Align->Vertical Centers
Forget about using different labels for this, it's nightmare. Use NSAttributedString with one UILabel it'll automatically center the text.
NSString *name = #"John\nMultiline\nSmith"; // your multiline string
NSString *title = #"Salesguy"; // some more strings
NSString *phone = #"555-55-55";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#\n%#\n%#", name, title, phone] attributes:#{NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue" size:17.0]}]; // construct the label with default parameters
[attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"HelveticaNeue-Bold" size:17.0] range:[attributedString.string rangeOfString:name]]; // add bold selection to the name part
myLabel.attributedText = attributedString;
My iOS app is not showing long attributed strings. I have a cell in a tableview which contains this textView. When the text is very long the tableview is unresponsive for a while but when it loads the text is not shown. All other cells are displayed fine. And the textView works fine with small text strings.
Here's the code:
descriptionCell = [tableView dequeueReusableCellWithIdentifier:#"CellAdDetailDescription"];
descriptionCell.bodyTextView.delegate = self;
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self.ad.body];
UIFont *cellFont;
cellFont = [UIFont fontWithName:#"HelveticaNeue" size:16.0];
NSDictionary *attributesDictionary;
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 10;
attributesDictionary = #{NSParagraphStyleAttributeName : paragraphStyle , NSFontAttributeName: cellFont};
[str addAttributes:attributesDictionary range:NSMakeRange(0, str.length)];
descriptionCell.bodyTextView.attributedText = str;
I pasted the long string here. I debugged and str is being loaded fine containing the desired text.
Whats wrong here?
What is the max allowed string length in UITextView?
EDIT: very odd, when trying selection in the textView, the text is being shown in the magnifying glass. I posted a video here.
Is it a bug in UITextView?
Here is the screenshot. The blank white at the bottom is the textView.
It could have something to do with the scrolling. If you are showing all the text (i.e. the text view is expanded to be as high as it needs to be, and so is the table view cell), the scrolling is done by the table view. If the text view is smaller, you have to scroll to see all the text - this might cause a conflict with the table view, which is also a scroll view.
It has been suggested that you disable the scrolling of the text view before adding the attributed text and reenable it afterwards. If you are showing the whole text in the table view, you can leave the text view scrolling disabled. In some cases, it will only work if scrolling is enabled. You should check this possibility as well.
I also had this issue (= very long attributed text didn't show up in the UITextView within an autosized UITableViewCell). I tried all accepted answers from here and from this question: UITextView not loading/showing the large text?. None did work.
However I then found out that - in my case - it just works fine on the device. So if you're still struggling with this kind of problem, don't rely on the iOS Simulator.
The reason that you UITextView not show all text because it Frame is too small so it truncates the text to fit with its frame. you can do follow step to show all text:
In your CustomUITableCell, override layoutSubView:
Use this function to calculate size of TextView that fit it content
[textView sizeThatFits:CGSizeMake(textView.frame.size.width, CGFLOAT_MAX)].height
After that, calculate and set new frame for TExtView (in uitableCell custom)
In the tableView:heightForCellAtIndexPath, calculate new size of textView (also height of cell) similar like above and return right height for cell.
I am trying to set custom font.
Font is working with UILabel.
When I tries to use for UITextView, its not working. UITextView is taking default font.
Any idea how to use custom font for UITextView.
Code I am using is as below.
attributedString = [[NSMutableAttributedString alloc] initWithString:text
attributes:#{ NSFontAttributeName : [UIFont fontWithName:#"GEDinarOne-Medium"
size:15], NSLigatureAttributeName: #2}];
abtUsText.attributedText = attributedString;
abtUsText.textAlignment = NSTextAlignmentRight;
abtUsText.textColor = cb60756;
I am using attributedString to get working with iOS 6.
iOS 7 is giving proper font, but iOS 6 is using default font for UITextView only.
Edit 1
I am using arabic font as GEDinarOne-Medium
Edit 2
This is not duplicate of what is mentioned above as my case is with arabic font and not english font. Mentioned in duplicate works with english custom fonts only.
After I investigate custom font (especially arabic) is not working UITextView using attributedString, below is what I did.
I just wanted to display the text in UITextView (for About Us in my app) as it has long text and I wanted scrolling option.
Below is what I did.
Add UIScrollView
Add UILabel inside UIScrollView
Assign text to UILabel.
abtUsLabel.attributedText = attributedString;
Make label sizeToFit
[abtUsLabel sizeToFit];
This is very important step, else it won't work.
Set scrollview contentSize as per label height.
[abtUsScrollView setContentSize:CGSizeMake(abtUsScrollView.frame.size.width, abtUsLabel.frame.size.height)];
Done... now if the Label is longer, we can scroll it.
I have a UIButton that I want resizing the text font depending on the size of the NSString. I want the maximum number of lines to be 3 and if the NSString length exceeds 3 lines then the font shrinks until all the text is shown. This is what I have so far:
button.titleLabel.textAlignment = NSTextAlignmentCenter;
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.adjustsFontSizeToFitWidth = YES;
button.titleLabel.numberOfLines = 3;
However this code does not do as I please; the UIButton text contains ... if the NSString exceeds 3 lines. Any ideas?
It is a bug to set the lineBreakMode when you want to the text to shrink. Just remove the line that sets lineBreakMode and it will work.
You also need to set the minimumScaleFactor of the label. The default is 0.