如何调整文本(字体)的大小以适合UISegmentedControl的UISegment? [英] How to resize text (font) to fit in UISegment of UISegmentedControl?

查看:339
本文介绍了如何调整文本(字体)的大小以适合UISegmentedControl的UISegment?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以减小字体大小以适合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.

我想保持每个段的大小相同,并希望减小可以适合UISegmentedControlUISegmentLabel (label)的字体大小,例如minimumscalefactorminimumfontsizeadjustsFontSizeToFitWidth.当包含在UISegmentedControl中时,这些属性不适用于标签.

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!!

提前谢谢!!

推荐答案

我发现了问题,实际上是我的错误!!!我同时设置了numberOfLines,adjustsFontSizeToFitWidth,minimumScaleFactorTitleTextAttributes.如果我们将titleTextAttribute设置为minimumScaleFactor,则无法使用.

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.

更新:(根据@ HawkEye1194在另一个答案的评论中的要求)

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;

这篇关于如何调整文本(字体)的大小以适合UISegmentedControl的UISegment?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆