我们可以一次编程地更改多个UILabel的字体(通过最少的代码行)吗?

时间:2013-05-30 08:03:21

标签: ios xcode uilabel

我是iOS编程的新手。以下是我面临的情况:我必须以编程方式创建一个包含25个标签的视图。在所有25个标签中,一些特征如颜色和字体大小等都很常见。

显而易见的解决方案是单独处理每个标签。但我很想知道有没有办法通过编写最小的编码为所有这些字体分配一个公共字体,或者我还可以选择单独处理每个标签。

我在网上研究过这个解决方案但没有成功。如果有替代方案,如果有人处理大数量的话,它将来会有所帮助。子视图。 感谢。

4 个答案:

答案 0 :(得分:3)

是的,你可以把它全部压成一条狭窄的线:

[view.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){ [([obj respondsToSelector:@selector(setFont:)] ? obj : nil) setFont:newFont];}];

答案 1 :(得分:1)

试试这个。

for( UIView *subVeiw in self.subviews)  {
   if([subVeiw isKindOfClass:[UILabel Class]])   {
     [(UILabel *)subVeiw setFont:[UIFont fontWithName:@"Times New Roman" size:20]];
     //You can set the Font like this.

     //Here is Your UILabel Object if you want to use it.
     UILabel *yourLabel  =  (UILabel *) subVeiw;
   }
}

答案 2 :(得分:0)

循环浏览视图的子视图,如下所示

for(UIView *v in yourView.subviews){
  if([v isMemberOfClass:[UILabel class]]){

  UILabel *label=(UILabel*)v
 // assign the required properties here.

}     }

答案 3 :(得分:0)

我之前尝试通过继承UILabel并在构造函数中设置所需属性并在设计器中更改标签类来实现相同的目标:

enter image description here

@implementation CustomLabel

//For Code initialized Labels
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code .. Set your properties here
      self.font = [UIFont fontWithName:@"TradeGothicLTStd-Bold" size:self.font.pointSize];
    }
    return self;
}

//For designer initialized labels
-(id)initWithCoder:(NSCoder *)aDecoder{
  self = [super initWithCoder:aDecoder];
  if (self) {
    // Initialization code .. Set your properties here
    self.font = [UIFont fontWithName:@"TradeGothicLTStd-Bold" size:self.font.pointSize];
  }
  return self;
}
相关问题