Change font type and size of UIActionSheet title string

后端 未结 6 766
误落风尘
误落风尘 2021-02-06 05:29

I have a UIActionSheet with title string \"DO: These tasks\". In the title string, the substring \"DO:\" should be Bold(with a particular font size) and the substring \"These ta

6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-06 06:11

    There is an internal property of UIActionSheet called _titleLabel so it is possible to get a reference and change the attributed string property of this label directly.

    Please be aware, that this is a private API in might be rejected in the App store.

    Here is how it works:

    - (NSAttributedString *) actionSheetAttributedTitle;
    {
        NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:@"Test"];
        UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:13.0f];
        [attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, attString.length)];
        [attString addAttribute:NSForegroundColorAttributeName
                      value:[UIColor blackColor]
                      range:NSMakeRange(0, attString.length)];
        return [attString copy];
    }
    
    - (void)willPresentActionSheet:(UIActionSheet *)actionSheet;
    {
        UILabel *sheetTitleLabel;
        if([actionSheet respondsToSelector:@selector(_titleLabel)]) {
            sheetTitleLabel = objc_msgSend(actionSheet, @selector(_titleLabel));
            sheetTitleLabel.attributedText = [self actionSheetAttributedTitle];
    
        }
    }
    

提交回复
热议问题