J'utilise un UisegmentedControl code> à l'intérieur d'un
uitoolbar code> comme bouton. Je ne peux pas utiliser d'uibuttonbaritem normal code>, car je dois définir le
TintColor code> et prendre en charge iOS 4.3.
Donc, j'utilise le code suivant:
NSString* label = @"My Label";
UIImage* image = [self addText:label toImage:[UIImage imageNamed:@"icon"]];
[_segmentedControl insertSegmentWithImage:image atIndex:0 animated:NO];
[_segmentedControl setWidth:image.size.width + 10]; // add some margin on the right
- (UIImage *)addText:(NSString *)text toImage:(UIImage *)image {
UIFont* font = [UIFont boldSystemFontOfSize: 12];
CGSize expectedSize = [text sizeWithFont:font];
[self retinaAwareUIGraphicsBeginImageContext:CGSizeMake(image.size.width + expectedSize.width + 10, image.size.height)];
[image drawAtPoint: CGPointZero];
[[UIColor whiteColor] set];
[text drawAtPoint: CGPointMake(30, 2) withFont:font];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (void)retinaAwareUIGraphicsBeginImageContext:(CGSize) size {
CGFloat scale = -1.0;
if (scale<0.0) {
UIScreen *screen = [UIScreen mainScreen];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0) {
scale = [screen scale];
}
else {
scale = 0.0;
}
}
if (scale>0.0) {
UIGraphicsBeginImageContextWithOptions(size, NO, scale);
}
else {
UIGraphicsBeginImageContext(size);
}
}
7 Réponses :
Essayez ceci:
UISegmentedControl* searchButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"", nil]]; [searchButton insertSegmentWithImage:[UIImage imageNamed:@"up_button.png"] atIndex:0 animated:YES]; [searchButton insertSegmentWithImage:[UIImage imageNamed:@"down_button.png"] atIndex:1 animated:YES]; searchButton.segmentedControlStyle = UISegmentedControlStyleBar; searchButton.tintColor = [UIColor colorWithRed:0.27 green:0.60 blue:0.20 alpha:1.00]; [searchButton setMomentary:YES];
Je ne pense pas que c'est possible. La documentation indique ce qui suit: Un contrôle segmenté peut afficher un titre (objet Nstring) ou une image (objet UIIMAGE). Si vous souhaitez définir le titre: [SearchButton Instetsewithtitle: @ "Up" AttinDex: 0 animé: Non]; [SearchButton Insertswithtitle: @ "Down" AttinDex: 1 animé: Non];
Vous pouvez le faire:
Ajouter une catégorie pour UIIMAGE: P>
in uiimage + uisegmentIndtext.h em> p> dans votre code de code segment: p> UIImage *imageWithText = [UIImage imageFromImage:[UIImage imageNamed:@"images/jobaids_tab_icon"] string:@"Hello", nil) color:[UIColor whiteColor]];
[_segmentedController setImage:imageWithText forSegmentAtIndex:0];
Réponse parfaite :) merci
Fonctionne bien, mais les tailles de police sont éteintes. La taille de texte semble devenir plus petite car la taille de la chaîne augmente
pour SWIFT.
segfont strong> est la police du texte intégré. p> ImageAlInment strong> est l'alignement de l'image apparaissant à gauche ou à droite du texte. p> SET
extension UIImage{
class func textEmbededImage(image: UIImage, string: String, color:UIColor, imageAlignment: Int = 0, segFont: UIFont? = nil) -> UIImage {
let font = segFont ?? UIFont.systemFont(ofSize: 16.0)
let expectedTextSize: CGSize = (string as NSString).size(attributes: [NSFontAttributeName: font])
let width: CGFloat = expectedTextSize.width + image.size.width + 5.0
let height: CGFloat = max(expectedTextSize.height, image.size.width)
let size: CGSize = CGSize(width: width, height: height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
let context: CGContext = UIGraphicsGetCurrentContext()!
context.setFillColor(color.cgColor)
let fontTopPosition: CGFloat = (height - expectedTextSize.height) / 2.0
let textOrigin: CGFloat = (imageAlignment == 0) ? image.size.width + 5 : 0
let textPoint: CGPoint = CGPoint.init(x: textOrigin, y: fontTopPosition)
string.draw(at: textPoint, withAttributes: [NSFontAttributeName: font])
let flipVertical: CGAffineTransform = CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: size.height)
context.concatenate(flipVertical)
let alignment: CGFloat = (imageAlignment == 0) ? 0.0 : expectedTextSize.width + 5.0
context.draw(image.cgImage!, in: CGRect.init(x: alignment, y: ((height - image.size.height) / 2.0), width: image.size.width, height: image.size.height))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
}
Merci d'avoir déplacé la réponse précédente à Swift!
Swift 5 strong> Pour ceux d'entre nous qui aiment utiliser une API moderne partout où il est possible: p>
Swift 5
Je devais ajouter une image sur le coin à droite, donc j'ai ajouté imageOffset. p>