【问题标题】:create multiple text attributes for single class为单个类创建多个文本属性
【发布时间】:2016-03-07 18:05:12
【问题描述】:

我是编程新手,我正在尝试创建一个类来设置大小、属性、颜色和对齐文本中心,因为我正在使用以下代码。我正在尝试做更多的事情,但是一旦我知道如何设置该类,就可以进行修改。

请帮忙。

@implementation OceansTabController
@synthesize textField;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.textField.text = @"\"The sea, once it casts its spell, holds one in its net of wonder forever.\"\nJacques Yves Cousteau\n\nOur majestic oceans\n\n";

    NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:textField.text];
    //
    //    UIFont *font_regular=[UIFont fontWithName:@"HelveticaNeue" size:14.0f];

    UIFont *font=[UIFont fontWithName:@"HelveticaNeue" size:14.0f];
    UIFont *font_italic=[UIFont fontWithName:@"HelveticaNeue-Italic" size:14.0f];
    UIFont *font_bold=[UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0f];
    UIFont *font_boldBlue=[UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0f];
    UIFont *font_boldLarge=[UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0f];
    UIFont *font_boldLargeCentered=[UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0f];

[attString addAttribute:NSFontAttributeName value:font_italic range:NSMakeRange(0, 75)];
[attString addAttribute:NSFontAttributeName value:font_boldBlue range:NSMakeRange(76, 21)];
[attString addAttribute:NSFontAttributeName value:font_boldLargeCentered range:NSMakeRange(98, 20)];

【问题讨论】:

    标签: ios objective-c uitextview uifont


    【解决方案1】:

    您可以创建 uitextfield 的子类。您可以在其中定义设置属性的方法,如下所示

    在 customTextField.h 中

    #import <UIKit/UIKit.h>
    
    @interface customtextField : UITextField
    
    - (void)setCustomTextFieldText:(NSString*)text;
    
    - (void)setCustomTextFieldAttributes;
    
    @end
    

    在 customTextField.m 中

    您可以将字体或任何属性定义为宏

    #import "customtextField.h"
    
    #define font [UIFont fontWithName:@"HelveticaNeue" size:14.0f]
    #define font_italic [UIFont fontWithName:@"HelveticaNeue-Italic" size:14.0f]
    #define font_bold [UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0f]
    #define font_boldBlue [UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0f]
    #define font_boldLarge [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0f]
    #define font_boldLargeCentered [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0f]
    
    @implementation customtextField
    
    - (void)setCustomTextFieldText:(NSString*)text{
    self.text = text;
    }
    
    - (void)setCustomTextFieldAttributes{
    NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:self.text];
    [attString addAttribute:NSFontAttributeName value:font_italic range:NSMakeRange(0, 75)];
    [attString addAttribute:NSFontAttributeName value:font_boldBlue range:NSMakeRange(76, 21)];
    [attString addAttribute:NSFontAttributeName value:font_boldLargeCentered range:NSMakeRange(98, 20)];}
    
    @end
    

    现在您可以将 customTextField 用作 uitextField 并且可以通过在您的类中导入 customTextField.h 文件来设置您需要的所有属性:

        customtextField * txtField = [[customtextField alloc] initWithFrame:CGRectMake(20, 100, 100, 40)];
    
        [txtField setCustomTextFieldText:@"your text for textfield"];
        [txtField setCustomTextFieldAttributes];
    
        [self.view addSubview:txtField];
    

    当然它会为你工作..祝你好运

    【讨论】:

    • 谢谢,但除非我不理解您的回复,否则我认为您可能误解了我的问题。我设置了 textView 类。但我希望能够将粗体字体、文本居中和字体颜色设置在一个可以与每个 [attString... 命令 一起使用的类中
    【解决方案2】:

    Shubham bairagi,感谢您的帮助。再读一遍,我确实意识到我并不完全清楚我想要什么,因为问题是我不知道所有的术语,所以要正确地提问。无论如何,我找到了解决方案:

    NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:textField.text];
    //
    //    UIFont *font_regular=[UIFont fontWithName:@"HelveticaNeue" size:14.0f];
    
    UIFont *font=[UIFont fontWithName:@"HelveticaNeue" size:14.0f];
    UIFont *font_italic=[UIFont fontWithName:@"HelveticaNeue-Italic" size:14.0f];
    UIFont *font_bold=[UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0f];
    UIFont *font_boldLarge=[UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0f];
    

    上面和我贴的一样,但是我加了这行来设置我想要的颜色

    UIColor *blueColor = [UIColor blueColor];
    

    设置变量(希望我使用了正确的术语)

    然后我将变量添加到下面的代码中。您会注意到我添加了一条具有相同范围的额外行。这给了我两行相同范围的代码(蓝色和粗体)。但是,我发现我没有使用 NSFontAttributeName,而是使用 NSForegroundColorAttributeName 来设置颜色。

    [attString addAttribute:NSFontAttributeName value:font_italic range:NSMakeRange(0, 138)];
    [attString addAttribute:NSFontAttributeName value:font_bold range:NSMakeRange(139, 21)];
    [attString addAttribute:NSForegroundColorAttributeName value:blueColor range:NSMakeRange(139, 21)];
    [attString addAttribute:NSFontAttributeName value:font_boldLarge range:NSMakeRange(161, 35)];
    

    如果我不清楚,请告诉我,我会再试一次。

    【讨论】:

      【解决方案3】:

      我只是想出了如何根据上述格式将文本居中

      我创建了变量:

      NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init] ;
      [paragraphStyle setAlignment:NSTextAlignmentCenter];
      

      并使用此代码将已加粗的文本添加到 SAME 范围内。

      [attString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(161, 35)];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多