My UILabel can do a line break according to the current \n . But if the line itself is too long, it won't be able to automatically do a line break. Can I do more configurations to my UILabel to achieve that?
And I've already used:
aLabel.lineBreakMode = UILineBreakModeWordWrap;
aLabel.numberOfLines = 0;
[aLabel setFont:[UIFont fontWithName:#"MarkerFelt-Wide" size:24]];
aLabel.textAlignment = UITextAlignmentCenter;
CGRect labelFrame = aLabel.bounds;
labelFrame.size = [words sizeWithFont:aLabel.font constrainedToSize:CGSizeMake(LABEL_WIDTH, 100000) lineBreakMode:aLabel.lineBreakMode];
aLabel.frame = CGRectMake(0, 0, aLabel.frame.size.width-10, labelFrame.size.height);
words is a NSString
Set Number of line as you want to set.
aLabel.numberOfLines = 2;
if you are having text in a paragraph and want to place it in a lable with differ lines no need to use UILineBreakModeWordWrap.. simply write one line code as in how many lines you want to place the text as
labobj.numberOfLines = 90;
Related
Here is the text present in my UILable...
"This list isn’t actually based on a straight popularity count; simply tallying the most-read verses results in a top 100 list that consists almost entirely of John 3:16 and verses from 1 Corinthians 13, Genesis 1, Romans 8, and Psalm 23. Our list instead considers instances in which BibleGateway.com users looked at three or fewer verses at one time."
How do i get the final line of text..ie.,"at three or fewer verses at one time." from my uilable.
Any solution is greatly appreciated.
I can give one idea . May this will help you.
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,50,200,350)];
myLabel.numberOfLines = 0;
myLabel.lineBreakMode = UILineBreakModeWordWrap;
myLabel.text = #"This is some text in a UILabel which is long enough to wrap around the lines in said UILabel. This is a test, this is only a test.";
[self.view addSubview:myLabel];
CGSize labelSize = [myLabel.text sizeWithFont:myLabel.font
constrainedToSize:myLabel.frame.size
lineBreakMode:UILineBreakModeWordWrap];
CGFloat labelHeight = labelSize.height;
NSLog(#"labelHeight = %f", labelHeight);
[myLabel release];
with above you can get height of label, then you can get the # of lines by dividing the height with some appropriate number which depends on the font size.
1) After this Get NSAttributedString from your UILabel.
2) From NSAttributedString you can get "\n" characters.
3) get last "\n" and extract value from this index to last index.
In my page based application i want to use line-break in uilabel,I try with the following coding,
#"The farmer and his wife were entranced with joy and danced cheerfully because of their goose lays \n a golden egg",
self.titlestring.frame = CGRectMake(53, 620, 0, 37);
self.titlestringcop.frame = CGRectMake(53, 620, 918, 37);
[self fadein];
[self.titlestring setText:self.dataobject1];
[self.titlestringcop setText:self.dataobject1];
textstring.numberOfLines = 0;
titlestringcop.numberOfLines = 0;
CGSize labelSize = [textstring.text sizeWithFont:textstring.font
constrainedToSize:textstring.frame.size
lineBreakMode:textstring.lineBreakMode];
CGSize labelSize1 = [titlestringcop.text sizeWithFont:titlestringcop.font
constrainedToSize:titlestringcop.frame.size
lineBreakMode:titlestringcop.lineBreakMode];
textstring.frame = CGRectMake(
textstring.frame.origin.x, textstring.frame.origin.y,
textstring.frame.size.width, labelSize.height);
titlestringcop.frame = CGRectMake(
titlestringcop.frame.origin.x, titlestringcop.frame.origin.y,
titlestringcop.frame.size.width, labelSize1.height);
But in doesn't showing any difference?Whats wrong in my code?
Try this line add these lines also in code & check:
textstring.lineBreakMode = UILineBreakModeWordWrap;
titlestringcop.lineBreakMode = UILineBreakModeWordWrap;
I am trying to adjust the size of the text in my UITextView so it fits. I have the following code:
NSString *storyTitle = [newsfeedToSubject.properties valueForKey:#"title"];
int currentFontSize = 18;
UIFont *currentFont = [UIFont fontWithName:kProximaNovaBold size:currentFontSize];
CGSize storyTitleSize = [storyTitle sizeWithFont:currentFont constrainedToSize:self.newsfeedStoryTitle_.frameSize lineBreakMode:UILineBreakModeWordWrap];
while (storyTitleSize.height >= self.newsfeedStoryTitle_.frameHeight){
currentFontSize--;
currentFont = [UIFont fontWithName:kProximaNovaBold size:currentFontSize];
storyTitleSize = [storyTitle sizeWithFont:currentFont constrainedToSize:self.newsfeedStoryTitle_.frameSize lineBreakMode:UILineBreakModeWordWrap];
}
[self.newsfeedStoryTitle_ setFont:currentFont];
[self.newsfeedStoryTitle_ setText:storyTitle];
[self.newsfeedStoryTitle_ setBackgroundColor:[UIColor redColor]];
However, here's what I am getting:
I tried with different line break mode and also set the autoresize to none, but it didn't help. Any idea?
Your while loop will not do what you want because you are using
storyTitleSize.height
to determine the size of the frame, which will not take into account the wrap around effect of the text. One suggestion would be to truncate the string and only display a certain amount of characters of the title. Otherwise, you will need to calculate how many line breaks you will have, based on the width of the frame and the length of the string, and use
while(storyTitleSize.height * numLineBreaks >= self.newsFeedStoryTitle_.frameHeight)
{
...
}
Hope that helps.
You can also dynamically resize your frame (UITextView) as in this thread
try these code
while (self.textView.contentSize.height >= frameHeight){
currentFontSize--;
currentFont = [UIFont fontWithName:kProximaNovaBold size:currentFontSize];
[self.textView setFont:currentFont];
self.textView.text = #"your string";
}
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How can i fix my scrollView dynamically when i have more than one textViews in it?
I made a scrollView in interface builder and put 3 textViews and 2 button in that. Sometimes textViews have more text and sometimes less. i managed to give them flexible frame size. but if top textView has more text then some of its text comes over 2nd textView. i tried this code. i think it should work but its not working properly.
storyTitle.text = sTitle;
storyTitle.font = [UIFont boldSystemFontOfSize:18];
date.text = sDate;
date.font = [UIFont systemFontOfSize:16];
summary.text = sSummary;
summary.font = [UIFont systemFontOfSize:16];
CGRect titleFrame = storyTitle.frame;
titleFrame.size.height = storyTitle.contentSize.height;
storyTitle.frame = titleFrame;
CGRect dateFrame = date.frame;
dateFrame.size.height = label.contentSize.height;
date.frame = dateFrame;
CGRect summaryFrame = summary.frame;
summaryFrame.size.height = summary.contentSize.height;
summary.frame = summaryFrame;
CGRect topTextViewFrame = storyTitle.frame;
CGRect middleTextViewFrame = date.frame;
NSLog(#"size: %d",topTextViewFrame.origin.y);
middleTextViewFrame.origin.y = topTextViewFrame.origin.y + topTextViewFrame.size.height;
CGRect bottomTextViewFrame = summary.frame;
bottomTextViewFrame.origin.y = middleTextViewFrame.origin.y + middleTextViewFrame.size.height;
// then adjust other controls based on these frames, for example:
CGRect myButtonFrame = detailButton.frame;
myButtonFrame.origin.y = bottomTextViewFrame.origin.y + bottomTextViewFrame.size.height;
CGRect myButtonFrame2 = fbButton.frame;
myButtonFrame2.origin.y = myButtonFrame.origin.y + myButtonFrame.size.height;
// finally adjust the contentSize of the scrollview assuming the button is the bottom element
CGSize csize = scrollView.contentSize;
csize.height = myButtonFrame2.origin.y + myButtonFrame2.size.height;
scrollView.contentSize = csize;
can anyone tell me whats wrong with this? and how should i right it so it ll work fine. thanx in advance
hi if your text is not editable by user the try using UILabel instead of UITextView.If you set numberOfLines property of UILabel = 0 then it can go to more that one line and then u can calculate the required height for the label by the method
CGSize size = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(label.frame.size.width, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap ];
the u can set the frame of label according to height you get form here like
label.frame = CGRectMake(label.frame.origin.x,label.frame.origin.y,label.frame.size.width,size.height);
and if you have such multiple labels the uyou can do following
CGSize size = [label1.text sizeWithFont:label1.font constrainedToSize:CGSizeMake(label1.frame.size.width, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap ];
label1.frame = CGRectMake(label1.frame.origin.x,label1.frame.origin.y,label1.frame.size.width,size.height);
size = [label2.text sizeWithFont:label2.font constrainedToSize:CGSizeMake(label2.frame.size.width, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap ];
label2.frame = CGRectMake(label2.frame.origin.x,label1.frame.origin.y+label1.frame.size.height+10,label2.frame.size.width,size.height);
size = [label3.text sizeWithFont:label3.font constrainedToSize:CGSizeMake(label3.frame.size.width, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap ];
label3.frame = CGRectMake(label3.frame.origin.x,label2.frame.origin.y+label2.frame.size.height+10,label3.frame.size.width,size.height);
and finaly you can adjust the content height of your scroll view according tho last label.
I'm trying to create a simple custom UIView wich contain a string drawn with a single font, but where the first character is slightly larger.
I thought this would be easily implemented with two UILabel:s placed next to eachother.
I use NSString sizeWithFont to measure my string to be able to lay it out correctly.
But I noticed that the font baseline in the returned rectangle varies with +/- 1 pixel depending on the font size I set.
Here is my code:
NSString* ctxt = [text substringToIndex:1];
NSString* ttxt = [text substringFromIndex:1];
CGSize sz = [ctxt sizeWithFont: cfont ];
clbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, sz.width, sz.height)];
clbl.text = ctxt;
clbl.font = cfont;
clbl.backgroundColor = [UIColor clearColor];
[contentView addSubview:clbl];
CGSize sz2 = [ttxt sizeWithFont: tfont];
tlbl = [[UILabel alloc] initWithFrame:CGRectMake(sz.width, (sz.height - sz2.height), sz2.width, sz2.height)];
tlbl.text = ttxt;
tlbl.font = tfont;
tlbl.backgroundColor = [UIColor clearColor];
[contentView addSubview:tlbl];
If I use 12.0 and 14.0 as sizes, it works fine.
But if I instead use 13.0 and 15.0, then the first character is 1 pixel too high.
Is this a known problem?
Any suggestions how to work around it?
Creating a UIWebView with a CSS and HTML page seems way overkill for this. and more work to handle dynamic strings. Is that what I'm expected to do?
Found the answer...
Ofcourse, I also have to check the descender value on the font, and compensate for that in the layout.
New rect for the second label is:
CGRectMake(sz.width, (sz.height - sz2.height) + floor(cfont.descender - tfont.descender), sz2.width, sz2.height)
floor() is to make sure it snaps to pixel position, or the font will look blurry