Change text size in different lines of textlabel cell uitableview - uitableview

I created a uitableview and in every cell there's a textLabel.
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = [NSString stringWithFormat:#"Hello\n how are you?"];
UIFont *myFont = [ UIFont fontWithName: #"Novel" size: 6.0 ];
cell.textLabel.font = myFont;
Can i set the font of "Hello" to 10 and the font of "how are you?" to 6?
Thank you!

Do as per the following:
UILabel *flabel = [[UILabel alloc]init];
flabel.text = #"Hello";
flabel.font = [UIFont fontWithName:flabel.text size:10];
UILabel *slabel = [[UILabel alloc]init];
slabel.text = #"How are you?";
slabel.font = [UIFont fontWithName:slabel.text size:6];
cell.textLabel.text = [NSString stringWithFormat:#"%#\n%#",flabel.text,slabel.text];
I have created two different labels having different font size and set it to cell textlabel by concatenation of both strings.
Hope this should work.

Related

Changing the color of a buttons text in iOS (objective-c)

I want to change the color of the text in my button to be blue on load. I also need only the first 9 letters (length of userName) of the buttons text to be blue, not all of the text. How do I do this?
Example:
This above image shows "#LisaLisa is following you". This is the buttons text. How do I make just "#LisaLisa" to be blue, with "is following you" staying black?
UIButton *someButton = [UIButton buttonWithType:UIButtonTypeSystem];
NSString *someUsername = #"#LisaLisa";
NSString *buttonText = [NSString stringWithFormat:#"%# is following you.", someUsername];
NSRange rangeToHighlight = [buttonText rangeOfString:someUsername];
NSDictionary *defaultAttributes = #{NSForegroundColorAttributeName: [UIColor blackColor]};
NSDictionary *highlightedAttributes = #{NSForegroundColorAttributeName: [UIColor blueColor]};
NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString alloc] initWithString:buttonText attributes:defaultAttributes];
[attributedTitle addAttributes:highlightedAttributes range:rangeToHighlight];
[someButton setAttributedTitle:attributedTitle forState:UIControlStateNormal];
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setLineBreakMode:NSLineBreakByWordWrapping];
UIColor *color1 = [UIColor redColor];
UIColor *color2 = [UIColor blueColor];
UIFont *font1 = [UIFont fontWithName:#"HelveticaNeue" size:20.0f];
UIFont *font2 = [UIFont fontWithName:#"HelveticaNeue-Light" size:20.0f];
NSDictionary *dict1 = #{NSFontAttributeName:font1,
NSForegroundColorAttributeName:color1 }; // Added line
NSDictionary *dict2 = #{NSFontAttributeName:font2,
NSForegroundColorAttributeName:color2 }; // Added line
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:senderName attributes:dict1]];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:#"posted to" attributes:dict2]];
[cell.profileIDButton setAttributedTitle:attString forState:UIControlStateNormal];
[[cell.profileIDButton titleLabel] setNumberOfLines:0];
[[cell.profileIDButton titleLabel] setLineBreakMode:NSLineBreakByWordWrapping];
This answer worked for me. I wasnt able to test Andre's answers yet.
UILabel * myLabel = [[UILabel alloc] init];//used for whole string
myLabel.numberOfLines = 0;
NSString * myUserName = #"#LisaLisa";//used for userName
//add button on UserName
UIButton * muButton = [[UIButton alloc] init];
myLabel.text = [NSString stringWithFormat:#"%#", myUserName];
myLabel = [self setDynamicLableFrame:myLabel fontSize:fontSize Width:width];
//Here width is your Label's Width. Because we have to make sure that our, our label's width is not greater than our device's width and fontSize is label's fontSize
muButton.frame = myLabel.frame;
myLabel.text = [NSString stringWithFormat:#"%# is following you", myUserName];
myLabel = [self setDynamicLableFrame:myLabel fontSize:fontSize Width:width];//used for making dynamic height of label
//For changing color of UserName
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString: myLabel.attributedText];
[text addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, myUserName.length)];
[myLabel setAttributedText: text];
Following method is used for making label's Dynamic Height & Width. Because we have to set Button's frame only on UserName ("LisaLisa").
//for setting the dynamic height of labels
-(UILabel *)setDynamicLableFrame:(UILabel*)myLabel fontSize:(float)size Width:(float)Width
{
CGSize possibleSize = [myLabel.text sizeWithFont:[UIFont fontWithName:REGULER_FONT size:size] constrainedToSize:CGSizeMake(300, 9999) lineBreakMode:NSLineBreakByWordWrapping];
CGRect newFrame = myLabel.frame;
newFrame.size.height = possibleSize.height;
if (possibleSize.width < Width) {
newFrame.size.width = possibleSize.width;
}else{
newFrame.size.width = Width;
}
myLabel.frame = newFrame;
return myLabel;
}
Hope, This is what you're looking for. Any concern get back to me.

Setting UILabel with attributed string as titleView nav controller results in weird behaviour

I'm trying to set the titleView of a UINavigationController with multiple lines in a UILabel.
I need the two lines to be different font sizes but perfectly aligned so I figured I'd use a attributed string to do so. Here's my code:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 480, 44)];
label.backgroundColor = [UIColor clearColor];
label.numberOfLines = 2;
label.font = [UIFont boldSystemFontOfSize: 14.0f];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
UIFont *fontSmall = [UIFont fontWithName:#"HelveticaNeue" size:11.0];
NSDictionary *attrsDictionarySmall =
[NSDictionary dictionaryWithObject:fontSmall
forKey:NSFontAttributeName];
NSAttributedString *attrSmallString =
[[NSAttributedString alloc] initWithString:#"small text"
attributes:attrsDictionarySmall];
UIFont *font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:14.0];
NSDictionary *attrsDictionary =
[NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];
NSAttributedString *attrString =
[[NSAttributedString alloc] initWithString:#"$200"
attributes:attrsDictionary];
label.text = [NSString stringWithFormat:#"%#\n%#",attrString,attrBTCString];
self.navigationItem.titleView = label;
However, this results in the following:
How do I fix that?
NSAttributedString has a NSString property which can be used here.
label.text = [NSString stringWithFormat:#"%#\n%#",attrString.string,attrSmallString.string];

AttributedString in UINavigationBar not showing both lines of text

I am trying to set a two line label in my UINavigationBar. When I do the following, it only shows one line (the part before the line break).
NSString *title = #"First line of title is bigger\nSecond line of title is smaller";
NSDictionary *attribute1 = #{NSForegroundColorAttributeName: [UIColor colorWithRed:148/255.0f green:147/255.0f blue:147/255.0f alpha:1],
NSFontAttributeName: [UIFont boldSystemFontOfSize: 14.0f]};
NSDictionary *attribute2 = #{NSForegroundColorAttributeName: [UIColor colorWithRed:148/255.0f green:147/255.0f blue:147/255.0f alpha:1],
NSFontAttributeName: [UIFont boldSystemFontOfSize: 10.0f]};
const NSRange line1Range = {0, 29};
const NSRange line2Range = {31, 30};
NSMutableAttributedString *attributedText =[[NSMutableAttributedString alloc] initWithString:title];
[attributedText setAttributes: attribute1 range:line1Range];
[attributedText setAttributes: attribute2 range:line2Range];
UILabel *label = [[UILabel alloc] init];
label.attributedText=attributedText;
self.navigationItem.titleView = label;
[self.navigationItem.titleView sizeToFit];
For comparison, the following shows both line. The following is for comparison only. The top version is what I need.
UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor clearColor];
label.numberOfLines = 2;
label.font = [UIFont boldSystemFontOfSize: 14.0f];
label.textColor = [UIColor colorWithRed:148/255.0f green:147/255.0f blue:147/255.0f alpha:1];
label.text = #"First line of title is bigger\nSecond line of title is smaller";
self.navigationItem.titleView = label;
[self.navigationItem.titleView sizeToFit];
Any help getting the top version to work correctly?
It appears that calling label.sizeToFit() before self.navigationItem.titleView = label displays the NSAttributedString title correctly.
(Swift, iOS8.1, XCode 6.1.1)
Easy problem, easy solution. Even if you are using attributedText instead of text, you still need to:
label.numberOfLines = 0;

How change size textlabel in UITableView

i have problem. I have dynamic tableview. So i need change text size in my textLabel.
I tried:
one:
cell.textLabel.font = [UIFont systemFontOfSize:30.0f];
two:
cell.textLabel.font = [UIFont boldSystemFontOfSize:12.0];
three:
UIFont *myFont = [ UIFont fontWithName: #"Arial" size: 30.0 ];
cell.textLabel.font = myFont;
But my textlabel not changing. So detailtextlabel - change perfectly but text label - no. What i do wrong? thanks
You can try with the following..
but make sure you write this code before u setting your text
cell.textLabel.text=#"Your TEXT Goes Here";
UIFont *myFont = [ UIFont fontWithName: #"Arial" size: 18.0 ];
cell.textLabel.font = myFont;
hope it helps.

How to adjust the UILabel width according to the String

I have a UITableView, filled with custom cells. The custom cell has many labels. One label it takes different size strings from the db. What I want is for the next label to start right after the first label (at the end of the string of first lable). I used this code:
cell.Manufactlbl.adjustsFontSizeToFitWidth=YES;
CGSize lLabelSIze = [coffeeObj.price sizeWithFont: cell.Manufactlbl.fontforWidth:cell.Manufactlbl.frame.size.width lineBreakMode:cell.Manufactlbl.lineBreakMode];
cell.Manufactlbl.frame = CGRectMake(cell.Manufactlbl.frame.origin.x, cell.Manufactlbl.frame.origin.y, cell.Manufactlbl.frame.size.width, lLabelSIze.height);
cell.Manufactlbl.font=[UIFont systemFontOfSize:12];
cell.Manufactlbl.text=coffeeObj.price;
cell.Typelbl.frame=CGRectMake(cell.Manufactlbl.frame.origin.x + cell.Manufactlbl.frame.size.width, cell.Typelbl.frame.origin.y, cell.Typelbl.frame.size.width, cell.Typelbl.frame.size.height);
cell.Typelbl.font=[UIFont systemFontOfSize:12];
cell.Typelbl.text=coffeeObj.instrument;`
But this does not work.
Try this.
CGSize size = [#"Your string"
sizeWithFont:[UIFont fontWithName:#"TimesNewRomanPS-BoldMT" size:22]
constrainedToSize:CGSizeMake(500, CGFLOAT_MAX)];
_Lable.frame = CGRectMake(05,05, 50, size.height);
hope it's helpful for you..
You can do something like this: size each label to fit (makes the label just big enough for the text), and then set the frame of each label to be dependent on the others. for example:
[labelOne sizeToFit];
[labelTwo sizeToFit];
labelTwo.frame = CGRectMake(CGRectGetMaxX(labelOne.frame), labelTwo.origin.y, labelTwo.frame.size.width, labelTwo.frame.size.height);
google NS(Attributed)String+Geometrics it solves it
Try this code :
NSString *text1 = #"example text1 ";
CGSize maximumLabelSize = CGSizeMake(236,9999);
CGSize expectedLabelSize = [text1 sizeWithFont:[UIFont systemFontOfSize:12]
constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeCharacterWrap];
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(50 ,0, expectedLabelSize.width, expectedLabelSize.height)];
label1.font = [UIFont systemFontOfSize:12];
label1.text = text1;
NSString *text2 = #"example Text2";
expectedLabelSize = [text2 sizeWithFont:[UIFont systemFontOfSize:12]
constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeCharacterWrap];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(label1.frame.origin.x + label1.frame.size.width ,0, expectedLabelSize.width, expectedLabelSize.height)];
label2.font = [UIFont systemFontOfSize:12];
label2.text = text2;
[self.view addSubview:label1];
[self.view addSubview:label2];

Resources