Change UIPickerView pickerView:viewForRow: attributes based on a certain condition - ios

I am trying to change the text color in viewForRow based on a certain condition. When I press a button the view changes to a different color but I would also like to change the colour of the text in the picker. I use 'viewForRow' because I have a custom view.
//adds subcategories to the wheel
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 44)];
label.backgroundColor = [UIColor clearColor];
if (!self.isBackgroundWhite)//Boolean that changes when the background changes
{
NSLog(#"Set white text");
label.textColor = [UIColor whiteColor];
}else{
label.textColor = [UIColor blackColor];
}
if (row == 1) {
label.text = [NSString stringWithFormat:#" %#",[self.servicesArray objectAtIndex:row]];
label.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:18];
}else{
label.text = [NSString stringWithFormat:#" -%#",[self.servicesArray objectAtIndex:row] ];
label.font = [UIFont fontWithName:kAppFont size:16];
}
return label;
}
EDIT: Thanks to #Rich I was able to spot my problem, I just need to call [self.pickerView reloadAllComponents];

If you only want to change the text color just use the other delegate method pickerView:attributedTitleForRow:forComponent:
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *title;
UIFont *font;
if (row == 1) {
title = [NSString stringWithFormat:#" %#",[self.servicesArray objectAtIndex:row]];
font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:18];
} else{
title = [NSString stringWithFormat:#" -%#",[self.servicesArray objectAtIndex:row] ];
font = [UIFont fontWithName:kAppFont size:16];
}
UIColor *textColor;
if (self.isBackgroundWhite) {
textColor = [UIColor blackColor];
} else {
NSLog(#"Set white text");
textColor = [UIColor whiteColor];
}
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.alignment = NSTextAlignmentLeft;
NSDictionary *attributes = #{NSForegroundColorAttributeName : textColor,
NSFontAttributeName : font,
NSParagraphStyleAttributeName : paragraphStyle};
return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}
Also a note that you should call [self.pickerView reloadAllComponents]; when changing isBackgroundWhite. I would do this by overriding the setter for backgroundWhite and when the boolean value changes reload the picker view.
EDIT:
There appears to be a 'bug' (intentional or not) in iOS 7 (works fine on iOS 6) with setting the UIFont on an NSAttributedString returned from the pickerView:attributedTitleForRow:forComponent: method. So while the above works for the text color the font does not change. Luckily you can get around this with the code in the question. See this answer for more info.

Related

How to resize text (font) to fit in UISegment of UISegmentedControl?

Is there any way to reduce font size that can be fit in single segment of UISegmentedControl ?
Have tried many thing something like,
[[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] adjustsFontSizeToFitWidth];
[[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] setMinimumScaleFactor:0.5];
AND
NSArray *arr = segment.subviews; // segment is UISegmentedControl object
for (int i = 0; i < arr.count; i++) {
UIView *aSegment = [arr objectAtIndex:i];
for (UILabel *label in aSegment.subviews) {
if ([label isKindOfClass:[UILabel class]]) {
UILabel *myLabel = (UILabel *)label;
[myLabel setNumberOfLines:0];
label.numberOfLines = 0;
label.adjustsFontSizeToFitWidth = YES;
label.minimumScaleFactor = 0.5;
}
}
}
able to set number of lines of label of segment like,
[[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] setNumberOfLines:0];
Can set single segment size as per content like,
segment.apportionsSegmentWidthsByContent = YES;
but every segment has different size in this case.
I want to keep same size of every segment and want to reduce font size that can be fit in UISegmentLabel (label) of UISegmentedControl something like minimumscalefactor or minimumfontsize or adjustsFontSizeToFitWidth. These properties is not working for label when contains in UISegmentedControl.
If any one can help to achieve this, it will be appreciated!!
Thanks in advance!!
I found the issue, Actually it was my mistake!!! I was setting numberOfLines,adjustsFontSizeToFitWidth,minimumScaleFactor and TitleTextAttributes toghether. If we set titleTextAttribute then minimumScaleFactor can't work.
Update : (As asked by #HawkEye1194 in comment of another answer)
I have end up with below solution,
//this will allow multiple lines in label contained by every segment in segmentedcontroller
[[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] setNumberOfLines:0];
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:option];
segment.frame = CGRectMake(20, 50, self.view.frame.size.width - 40, 50);
segment.tintColor = [UIColor grayColor];
segment.selectedSegmentIndex = 0;
segment.backgroundColor = [UIColor whiteColor];
segment.tag = segmentedControllerBaseTag;
[segment addTarget:self action:#selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
[segment setTitleTextAttributes:#{NSFontAttributeName :[UIFont fontWithName:#"HelveticaNeue" size:17.0], NSForegroundColorAttributeName : [UIColor darkGrayColor] } forState:UIControlStateNormal];
[segment setTitleTextAttributes:#{NSFontAttributeName : [UIFont fontWithName:#"HelveticaNeue" size:17.0],NSForegroundColorAttributeName : [UIColor whiteColor]} forState:UIControlStateSelected];
If your not setting title textattribute as above then you can use below code
// ********** if titletextattributes are not set then below method works ***********
for(uint i=0;i<[segment subviews].count;i++)
{
for(UIView *view in [[[segment subviews] objectAtIndex:i] subviews])
{
if([view isKindOfClass:[UILabel class]])
{
[(UILabel*)view setNumberOfLines:0];
[(UILabel*)view setAdjustsFontSizeToFitWidth:YES];
[(UILabel*)view setMinimumScaleFactor:0.7];
}
}
}
You can adjust single segment's size as per it's content by below code,
//*************** adjust single segment size as per content
segment.apportionsSegmentWidthsByContent = YES;
Try this, I hope this will help you and you will get an idea how this works-
I have a UISegmentedControl i.e. _userProfileSagmentOutlet having three segments. Here is sample code-
CGFloat fontSize = 15;
[_userProfileSagmentOutlet setTitleTextAttributes:#{NSFontAttributeName:[UIFont fontWithName:#"Roboto-medium" size:fontSize],
NSForegroundColorAttributeName:[UIColor whiteColor]}
forState:UIControlStateSelected];
[_userProfileSagmentOutlet setTitleTextAttributes:#{NSFontAttributeName:[UIFont fontWithName:#"Roboto-medium" size:fontSize],
NSForegroundColorAttributeName:[UIColor whiteColor]}
forState:UIControlStateNormal];
this is the previous code which truncate tail of title like below image-
here is the main logic which fit each title in segments with same font size-
CGFloat fontSize = 15;
NSAttributedString* firstTitle = [[NSAttributedString alloc] initWithString:#"Membership History" attributes:#{NSFontAttributeName: [UIFont fontWithName:#"Roboto-medium" size:fontSize]}];
NSAttributedString* secondTitle = [[NSAttributedString alloc] initWithString:#"Event History" attributes:#{NSFontAttributeName: [UIFont fontWithName:#"Roboto-medium" size:fontSize]}];
NSAttributedString* thirdTitle = [[NSAttributedString alloc] initWithString:#"Booked Classes" attributes:#{NSFontAttributeName: [UIFont fontWithName:#"Roboto-medium" size:fontSize]}];
float maxW=MAX(MAX(firstTitle.size.width, secondTitle.size.width), thirdTitle.size.width);
while (maxW > _userProfileSagmentOutlet.subviews[0].frame.size.width) {
fontSize--;
firstTitle = [[NSAttributedString alloc] initWithString:#"Membership History" attributes:#{NSFontAttributeName: [UIFont fontWithName:#"Roboto-medium" size:fontSize]}];
secondTitle = [[NSAttributedString alloc] initWithString:#"Event History" attributes:#{NSFontAttributeName: [UIFont fontWithName:#"Roboto-medium" size:fontSize]}];
thirdTitle = [[NSAttributedString alloc] initWithString:#"Booked Classes" attributes:#{NSFontAttributeName: [UIFont fontWithName:#"Roboto-medium" size:fontSize]}];
maxW=MAX(MAX(firstTitle.size.width, secondTitle.size.width), thirdTitle.size.width);
}
[_userProfileSagmentOutlet setTitleTextAttributes:#{NSFontAttributeName:[UIFont fontWithName:#"Roboto-medium" size:fontSize],
NSForegroundColorAttributeName:[UIColor whiteColor]}
forState:UIControlStateSelected];
[_userProfileSagmentOutlet setTitleTextAttributes:#{NSFontAttributeName:[UIFont fontWithName:#"Roboto-medium" size:fontSize],
NSForegroundColorAttributeName:[UIColor whiteColor]}
forState:UIControlStateNormal];
after using this code image look like this(same font size and text fit to segment and works fine)-
Here is Swift extension if someone needed-
var fontSize:CGFloat = 15.0;
var firstTitle = NSMutableAttributedString.init(string: "Membership History", attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)])
var secondTitle = NSMutableAttributedString.init(string: "Events History" ,attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)]);
var thirdTitle = NSMutableAttributedString.init(string: "Booked Classes" ,attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)]);
var maxW:CGFloat = max(max(firstTitle.size().width, secondTitle.size().width), thirdTitle.size().width);
while (maxW > userProfileSagmentOutlet.subviews[0].frame.size.width) {
fontSize--;
firstTitle = NSMutableAttributedString.init(string: "Membership History", attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)])
secondTitle = NSMutableAttributedString.init(string: "Events History" ,attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)]);
thirdTitle = NSMutableAttributedString.init(string: "Booked Classes" ,attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)]);
maxW = max(max(firstTitle.size().width, secondTitle.size().width), thirdTitle.size().width);
}
userProfileSagmentOutlet.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(fontSize),NSForegroundColorAttributeName:UIColor.whiteColor()], forState:UIControlState.Normal)
userProfileSagmentOutlet.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(fontSize),NSForegroundColorAttributeName:UIColor.whiteColor()], forState:UIControlState.Selected)
SWIFT 4
allow multiple lines in label
if #available(iOS 9.0, *) {
UILabel.appearance(whenContainedInInstancesOf: [UISegmentedControl.self]).numberOfLines = 0
}
The most simple solution in modern Swift would be
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
_ = Your_UISegmentControl.subviews.compactMap { $0.subviews.compactMap {
($0 as? UILabel)?.adjustsFontSizeToFitWidth = true
($0 as? UILabel)?.minimumScaleFactor = 0.5
}}
}
I ran this in the view controller, and called it after viewDidAppear
func autoshrinkSegmentFontSize() {
for subview in segments.subviews {
for label in subview.subviews {
if let myLabel = subview as? UILabel {
myLabel.adjustsFontSizeToFitWidth = true
myLabel.minimumScaleFactor = 0.5
}
}
}
}

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.

How to change the font family & style of textview in iOS?

I want to change the font style & family of the UITextView .Actually i will pick the font family from picker then change the font family of UITextView.Please tell me how to do this?
in that assumption I submit my answer, customize yourself else if any queries ask here
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component{
// here you can get index for selected Row
[yourTextView setText:[pickerArray objectAtIndex:row]];
[yourTextView setFont:[UIFont fontWithName:#"Helvetica Neue" size:18.0f]];
yourTextView.textColor = [UIColor whiteColor];
}
for picker tutorial
try this for showing name of family fontname in picker
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel* labelFontName = (UILabel*)view;
if (!labelFontName ){
labelFontName = [[UILabel alloc] init];
labelFontName .adjustsFontSizeToFitWidth = YES;
labelFontName .TextAlignment = NSTextAlignmentCenter;
labelFontName .font = [UIFont fontWithName:#"Arial" size:18];
}
NSString *stringForTitle= [NSString stringWithFormat:#"%#", [arrayOfYourPickerElements objectAtIndex:row]];// Fill the label text here
labelFontName .text = stringForTitle;
return labelFontName ;
}
after selecting fontName:
yourTextView.font = [UIFont fontWithName:label size:17];

iOS change font size programmatically not work

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = [UIColor clearColor];
if ([[UIDevice currentDevice].systemVersion floatValue] < 7.0f) {
self.textLabel.backgroundColor = [UIColor whiteColor];
}
_topUserNewName= [[UILabel alloc] init];
_topUserNewName.textAlignment = NSTextAlignmentLeft;
_topUserNewName.backgroundColor = [UIColor clearColor];
_topUserNewName.font = [UIFont fontWithName:#"HelvaticaNeue-Regular" size:5.0];
_topUserNewName.textColor = [UIColor colorWithRed:(47/255.0) green:(55/255.0) blue:(65/255.0) alpha:1];
_topUserNewName.frame = CGRectMake(66, 8, 215, 21);
_topUserName= [[UILabel alloc] init];
_topUserName.textAlignment = NSTextAlignmentLeft;
_topUserName.backgroundColor = [UIColor clearColor];
_topUserName.font = [UIFont fontWithName:#"HelvaticaNeue-Regular" size:10.0];
_topUserName.textColor = [UIColor colorWithRed:(145/255.0) green:(212/255.0) blue:(210/255.0) alpha:1];
_topUserName.frame = CGRectMake(66, 37, 210, 21);
[self.contentView addSubview:_topUserNewName];
[self.contentView addSubview:_topUserName];
self.selectionStyle = UITableViewCellSelectionStyleNone;
}
return self;
}
This codes in my table cell class. I want to change font size with programmatically right here when i change size nothing change ?
i researched on google this rule is right but not work any idea ?
The name HelveticaNeue-Regulardoes not actually exist. This should be [UIFont fontWithName:#"HelvaticaNeue" size:10.0];.
You can see the list of all font names here : http://iosfonts.com/
Please make sure you are passing the right font family and font face..try this
[UIFont fontWithName:#"HelveticaNeue-Bold" size:10.0];
HelvticaNeue-Regular face is not suported by ios, you can check all supported fonts by enumerating as:
for (NSString *familyName in [UIFont familyNames]){ NSLog(#"Font Family = %#",familyName);
for (NSString *fontName in
[UIFont fontNamesForFamilyName:familyName]){ NSLog(#"\t%#", fontName);
}
}
}
/===FOR IOS8===/
static NSString *_myCustomFontName;
+ (NSString *)myCustomFontName
{
if ( !_myCustomFontName )
{
NSArray *arr = [UIFont fontNamesForFamilyName:#"Custom Font Family"];
// I know I only have one font in this family
if ( [arr count] > 0 )
_myCustomFontName = arr[0];
}
return _myCustomFontName;
}

2 lines (multiline) picker for long text

why can i set the Row hight or multi lines for a picker,
the problem i have long texts for my picker, or gives an line break in picker... xcode 5 ...
i get the values for the picker from an array.
my code for my custom Picker is:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *label = [[UILabel alloc] init];
CGRect frame = CGRectMake(0,0,265,40);
label.backgroundColor = PickColor;
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:18 ];
label.textAlignment = NSTextAlignmentCenter;
if(component == 0)
{
label.text = [_vergehen objectAtIndex:row];
}
return label;
}
thanks for help
Try this code
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return 50;
}
then
- (UIView *)pickerView:(UIPickerView *)thePickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 310, 50)];
lblTitle.numberOfLines=2;
lblTitle.textColor=[UIColor blackColor];
lblTitle.font=[UIFont systemFontOfSize:14];
lblTitle.text=[selectionList objectAtIndex:row];
return lblTitle;
}
Because you're creating a label, you might try this property of UILabel:
// UILabel wraps text across multiple lines
label.lineBreakMode = UILineBreakModeWordWrap;
If you set label.numberOfLines = 0, you should be able to get any number of lines placed within the label.
UILineBreakModeWordWrap is deprecated deprecated in iOS 6.0
textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.numberOfLines = 0;

Resources