J'ai créé une icône flottante en utilisant le code suivant.Le texte n'est pas centré Aligné dans le bouton de l'interface utilisateur arrondie - ObjectiveC
- (void)createFloatingButton
{
UIButton *floatingButton = [UIButton buttonWithType:UIButtonTypeCustom];
floatingButton.frame = CGRectMake(kScreenWidth-kFloatingButtonSize-kFloatingButtonSize/2, self.view.frame.size.height-kFloatingButtonSize-kFloatingButtonSize/2, kFloatingButtonSize,kFloatingButtonSize);
[floatingButton setBackgroundColor:kNavColor];
floatingButton.layer.cornerRadius = kFloatingButtonSize/2;
//Configre Font and Text
[floatingButton setTitle:@"+" forState:UIControlStateNormal];
floatingButton.titleLabel.font = [UIFont systemFontOfSize:30];
[floatingButton addTarget:self action:@selector(floatingButtonTapped) forControlEvents:UIControlEventTouchUpInside];
//Mange Alignment
floatingButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
floatingButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//Add shadow
floatingButton.layer.shadowColor = [[UIColor blackColor] CGColor];
floatingButton.layer.shadowOffset = CGSizeMake(0, 2.0f);
floatingButton.layer.shadowOpacity = 0.6f;
floatingButton.layer.shadowRadius = 3.0f;
floatingButton.layer.masksToBounds = NO;
[self.view addSubview:floatingButton];
}
Ma sortie est comme ça:
Je ne suis pas en mesure d'apporter le +
dans le centre.
Vous pouvez utiliser image avec + au lieu de tout faire. Faire une image avec + lui-même. C'est facile, pas de problème pour + –
@Jecky Dans Image, je dois ajouter un geste tap ou si je mets UIImage Derrière UIButton le code augmente Je ne veux pas utiliser Imgaege. – Dalvik
"floatingButton.titleLabel.textAlignment = NSTextAlignmentCenter;" devrait être utile. – alicanbatur