Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios7 如何检查按钮形状设置是否已启用?_Ios7_Accessibility - Fatal编程技术网

Ios7 如何检查按钮形状设置是否已启用?

Ios7 如何检查按钮形状设置是否已启用?,ios7,accessibility,Ios7,Accessibility,iOS 7.1包含一个新的可访问性设置调用按钮形状,该设置会导致一些按钮文本自动加下划线。是否有方法检测此模式,或为单个ui按钮s自定义此模式 (这是为了允许更改按钮标签,如破折号或下划线,以便在加下划线时,它们看起来不像等号等。)我也遇到了同样的问题,但没有找到正式的解决方案。因此,在苹果发布解决方案之前,我找到的唯一解决办法是将UIToolbar渲染成图像,并检查按钮是否有下划线: + (BOOL)isUsesButtonShapes { BOOL result = FALSE;

iOS 7.1包含一个新的可访问性设置调用按钮形状,该设置会导致一些按钮文本自动加下划线。是否有方法检测此模式,或为单个
ui按钮
s自定义此模式


(这是为了允许更改按钮标签,如破折号或下划线,以便在加下划线时,它们看起来不像等号等。)

我也遇到了同样的问题,但没有找到正式的解决方案。因此,在苹果发布解决方案之前,我找到的唯一解决办法是将UIToolbar渲染成图像,并检查按钮是否有下划线:

+ (BOOL)isUsesButtonShapes {
    BOOL result = FALSE;

    CGRect rect = CGRectMake(0, 0, 320, 44);
    CGPoint point = CGPointMake(26, 33);

    UIToolbar *toolbar = [[[UIToolbar alloc] initWithFrame:rect] autorelease];
    toolbar.backgroundColor = [UIColor whiteColor];
    toolbar.tintColor = [UIColor darkGrayColor];
    toolbar.barTintColor = [UIColor whiteColor];
    [toolbar setItems:@[[[[UIBarButtonItem alloc] initWithTitle:@"Test" style:UIBarButtonItemStyleBordered target:nil action:nil] autorelease]]];
    toolbar.barStyle = UIBarStyleDefault;
    toolbar.translucent = FALSE;

    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    [toolbar.layer renderInContext:context];

    int bpr = CGBitmapContextGetBytesPerRow(context);
    unsigned char *data = CGBitmapContextGetData(context);
    if (data != NULL) {
        int offset = (int) (bpr * point.y + 4 * point.x);
        int blue = data[offset + 0];

        result = blue < 250;
    }

    UIGraphicsEndImageContext();

    return result;
}
+(BOOL)是使用按钮形状{
布尔结果=假;
CGRect rect=CGRectMake(0,0320,44);
CGPoint=CGPointMake(26,33);
UIToolbar*工具栏=[[[UIToolbar alloc]initWithFrame:rect]autorelease];
toolbar.backgroundColor=[UIColor whiteColor];
toolbar.tintColor=[UIColor darkGrayColor];
toolbar.barTintColor=[UIColor-whiteColor];
[工具栏设置项:@[[[UIBarButtonItem alloc]initWithTitle:@“测试”样式:UIBarButtonItemStyleBordered目标:无操作:无]自动释放]];
toolbar.barStyle=UIBarStyleDefault;
toolbar.translucent=FALSE;
UIGraphicsBeginImageContext(矩形大小);
CGContextRef context=UIGraphicsGetCurrentContext();
[toolbar.layer renderInContext:context];
int bpr=CGBitmapContextGetBytesPerRow(上下文);
unsigned char*data=CGBitmapContextGetData(上下文);
如果(数据!=NULL){
整数偏移=(整数)(bpr*点y+4*点x);
蓝色整数=数据[偏移量+0];
结果=蓝色<250;
}
UIGraphicsSendImageContext();
返回结果;
}
它基本上只是将UIToolbar渲染为图像:


然后检查“T”下的像素中是否有下划线。我知道,如果苹果改变UIToolbar的渲染方式,这一点很容易被打破。但也许这种方法可以改进,至少比什么都没有好?很抱歉,这不是一个好的解决方案,但我还没有找到更好的解决方案。

这只是半相关的,但我有点“自己滚动”按钮形状,并通过设置菜单向用户提供该选项

我很抱歉在这个问题上没有做到“一针见血”,但这正是我思考同一个问题的结果

(该示例设置为始终使用半圆表示圆角,无论大小-请根据需要进行修改)


看起来您可以请求按钮标签的属性并测试它是否包含NSUnderlineStyleAttributeName属性。如果删除NSUnderlineStyleAttributeName属性,系统会将其放回原位,因此关键在于显式地将标签的UnderlineStyleAttributeName属性设置为0。我已将以下内容添加到自定义按钮:

- (void) adjustLabelProperties  // override underline attribute
{
    NSMutableAttributedString   *attributedText = [self.titleLabel.attributedText mutableCopy];

    [attributedText addAttribute: NSUnderlineStyleAttributeName value: @(0) range: NSMakeRange(0, [attributedText length])];
    self.titleLabel.attributedText = attributedText;
}

我知道这是一个老问题,但这个代码是有效的。在iOS 9.3中测试

NSMutableAttributedString *attrStr = [btn.titleLabel.attributedText mutableCopy];
[attrStr enumerateAttributesInRange:NSMakeRange(0, [attrStr length])
                            options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
                         usingBlock:^(NSDictionary *attributes, NSRange range, BOOL *stop) {
                             NSMutableDictionary *mutableAttributes = [NSMutableDictionary dictionaryWithDictionary:attributes];
                             if([mutableAttributes objectForKey:NSUnderlineStyleAttributeName] != nil) {
                                 //It's enabled for this button
                             }
                         }];
禁用特定按钮的按钮形状的步骤

[btn.titleLabel.attributedText addAttribute: NSUnderlineStyleAttributeName value: @(0) range: NSMakeRange(0, [attributedText length])];

老问题,但希望这对某人有所帮助。仍然没有用于检查iOS上是否启用按钮形状的内置方法,因此我们添加了以下内容:

#pragma mark - Accessibility

/**
 * There's currently no built-in way to ascertain whether button shapes is enabled.
 * But we want to manually add shapes to certain buttons when it is.
 */

static BOOL _accessibilityButtonShapesEnabled = NO;

+ (BOOL)accessibilityButtonShapesEnabled {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self checkIfButtonShapesEnabled];
    });

    return _accessibilityButtonShapesEnabled;
}

+ (void)checkIfButtonShapesEnabled {
    UIButton *testButton = [[UIButton alloc] init];
    [testButton setTitle:@"Button Shapes" forState:UIControlStateNormal];

    _accessibilityButtonShapesEnabled = (BOOL)[(NSDictionary *)[testButton.titleLabel.attributedText attributesAtIndex:0 effectiveRange:nil] valueForKey:NSUnderlineStyleAttributeName];
}
由于在应用程序运行时,如果按钮形状被禁用/启用,也不会有通知,因此我们在
applicationIDBecomeActive:
中运行
checkIfButtonShapesEnabled
,并在值发生更改时推送我们自己的通知。这应该在所有情况下都有效,因为目前无法将按钮形状切换添加到“辅助功能快捷方式”。

我将代码从post转换为Swift(4.2):

用法:

if UIAccessibility.isButtonShapesEnabled {
    // Apply button shapes style to custom button...
}
在iOS 12中测试和工作


最初是在我自己的问题中发布的:

从iOS 14开始,您可以使用
UIAccessibility.buttonShapesEnabled
UIAccessibilityButtonShapesEnabled()
,启用设置后将为真。

我自己在文档或标题中都没有找到任何内容。我建议你归档一个雷达,这样我就可以复制它;)+1.希望在下一个SDK发布时,苹果将提供以下内容:
UIKIT\u EXTERN BOOL UIAccessibilityAreButtonShapesEnabled()NS\u可用的IOS(7\u 1)对于小型游戏/应用程序,这可能是一个合理的静态检查,但在大型项目中非常不适合和草率。不过,这仍然是一个不错的发现。所有的路都通向保罗。。。
import UIKit

public extension UIAccessibility {

    public static var isButtonShapesEnabled: Bool {
        let button = UIButton()
        button.setTitle("Button Shapes", for: .normal)
        return button.titleLabel?.attributedText?.attribute(NSAttributedString.Key.underlineStyle, at: 0, effectiveRange: nil) != nil
    }

}
if UIAccessibility.isButtonShapesEnabled {
    // Apply button shapes style to custom button...
}