IOS UIButton with multiple shortcuts

I have a user interface button on which I would like to put two labels on it, just as a cell has header text and long text.

I would like the button to have a larger font for the main text and have a smaller detail text under it.

Is it possible? I tried to put several lines on a button, but I need to have different text sizes for each line, so setting the lineBreakMode and numberOfLines to titleLabel really does not work.

+6
source share
3 answers

Here is the code we finally used. Help from John Wang.

Thanks everyone for the suggestions!

// Formats a label to add to a button. Supports multiline buttons // Parameters: // button - the button to add the label to // height - height of the label. usual value is 44 // offset - the offset from the top of the button // labelText - the text for the label // color - color of the text // formatAsBold - YES = bold NO = normal weight // tagNumber - tag for the label - (void) formatLabelForButton: (UIButton *) button withHeight: (double) height andVerticalOffset: (double) offset andText: (NSString *) labelText withFontSize: (double) fontSize withFontColor: (UIColor *) color andBoldFont:(BOOL) formatAsBold withTag: (NSInteger) tagNumber { // Get width of button double buttonWidth= button.frame.size.width; // Initialize buttonLabel UILabel *buttonLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, offset, buttonWidth, height)]; // Set font size and weight of label if (formatAsBold) { buttonLabel.font = [UIFont boldSystemFontOfSize:fontSize]; } else { buttonLabel.font = [UIFont systemFontOfSize:fontSize]; } // set font color of label buttonLabel.textColor = color; // Set background color, text, tag, and font buttonLabel.backgroundColor = [UIColor clearColor]; buttonLabel.text = labelText; buttonLabel.tag = tagNumber; // Center label buttonLabel.textAlignment = UITextAlignmentCenter; // Add label to button [button addSubview:buttonLabel]; [buttonLabel autorelease]; } // End formatLabelForButton 
+6
source

The trick I would recommend is to install a UIButton with a transparent interior on top of UILabels. I have used this trick before and, although it may present some problems in terms of service and i18n, it works like a charm.

Here is an example of 5 minutes using the above sentence. Label behind button

Given more time, you can make a better mark with round corners.

+4
source

You should be able to add routines to it. Since all of this is a view, everyone can potentially have a view.

I would subclass it and put labels in it in a subclass. You can then expand the properties of the text and subtext to change their values.

Not to say that it can work 100%. But from the head. UIView may have subviews

+1
source

Source: https://habr.com/ru/post/887159/


All Articles