如何使TTButtons标题左对齐?

时间:2011-07-04 06:52:29

标签: three20 styles

我知道如何创建TTButton:

TTButton *epriceButton = [TTButton buttonWithStyle:@"epriceButton:"];

我有一个方法:

- (TTStyle *)epriceButton:(UIControlState)state {
    TTShape* shape;
    shape = [TTRoundedRectangleShape shapeWithRadius:4.5];
    UIColor* tintColor = RGBCOLOR(8, 101, 191);
    return [TTSTYLESHEET toolbarButtonForState:state shape:shape tintColor:tintColor font:nil]; 
}

这很好用。我想从该按钮最后一件事就是让它的标题保持对齐。我是three20库的新手,所以我觉得我不明白样式是如何工作的。我发现每个TTStyle都有next:方法。但是多种风格如何协同工作?

1 个答案:

答案 0 :(得分:0)

按钮的文本对齐方式在-(TTStyle*)toolbarButtonForState: shape: tintColor: font:内定义。因此,您需要创建自己的此方法版本。

如果您查看原始文件,您会发现TTTextStyle是最后添加的样式。 TTTextStyle让您使用方便的初始化程序

设置textAlignment
+ (TTTextStyle*)styleWithFont:(UIFont*)font color:(UIColor*)color
              minimumFontSize:(CGFloat)minimumFontSize
                  shadowColor:(UIColor*)shadowColor shadowOffset:(CGSize)shadowOffset
                textAlignment:(UITextAlignment)textAlignment
            verticalAlignment:(UIControlContentVerticalAlignment)verticalAlignment
                lineBreakMode:(UILineBreakMode)lineBreakMode numberOfLines:(NSInteger)numberOfLines
                         next:(TTStyle*)next

所以你的实现可能如下:

- (TTStyle*)myButtonForState:(UIControlState)state shape:(TTShape*)shape
                        tintColor:(UIColor*)tintColor font:(UIFont*)font {
  UIColor* stateTintColor = [self toolbarButtonColorWithTintColor:tintColor forState:state];
  UIColor* stateTextColor = [self toolbarButtonTextColorForState:state];

  return
  [TTShapeStyle styleWithShape:shape next:
   [TTInsetStyle styleWithInset:UIEdgeInsetsMake(2, 0, 1, 0) next:
    [TTShadowStyle styleWithColor:RGBACOLOR(255,255,255,0.18) blur:0 offset:CGSizeMake(0, 1) next:
     [TTReflectiveFillStyle styleWithColor:stateTintColor next:
      [TTBevelBorderStyle styleWithHighlight:[stateTintColor multiplyHue:1 saturation:0.9 value:0.7]
                                      shadow:[stateTintColor multiplyHue:1 saturation:0.5 value:0.6]
                                       width:1 lightSource:270 next:
       [TTInsetStyle styleWithInset:UIEdgeInsetsMake(0, -1, 0, -1) next:
        [TTBevelBorderStyle styleWithHighlight:nil shadow:RGBACOLOR(0,0,0,0.15)
                                         width:1 lightSource:270 next:
         [TTBoxStyle styleWithPadding:UIEdgeInsetsMake(8, 8, 8, 8) next:
          [TTImageStyle styleWithImageURL:nil defaultImage:nil
                              contentMode:UIViewContentModeScaleToFill size:CGSizeZero next:
           [TTTextStyle styleWithFont:font color:stateTextColor
                                      minimumFontSize:14.0
                                          shadowColor:[UIColor colorWithWhite:0 alpha:0.4] shadowOffset:CGSizeMake(0, -1)
                                        textAlignment:UITextAlignmentLeft
                                    verticalAlignment:UIControlContentVerticalAlignmentCenter
                                        lineBreakMode:UILineBreakModeWordWrap numberOfLines:1
                                                 next:nil]]]]]]]]]];
}